uniapp 有自带的 websocket 不需要安装其他依赖
示例代码:
<template>
<view class="container">
</view>
</template>
<script>
export default{
data(){
return {
socketObj: null
}
},
onLoad(){
this.socketObj = uni.connectSocket({
url: 'wss://www.example.com/socket',
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET'
});
this.socketObj.onSocketMessage(function (res) {
console.log('收到服务器内容:' + res.data);
});
},
onUnload(){
uni.closeSocket(this.socketObj)
}
}
</script>