为什么采用变量接收的websoket,建立成功乐但报错:
sendDeviceInfoSocket.onOpen is not a function
我知道了
uni.connectSocket({
// 里面必须放 success / fail / complete 参数中的一个
})
只能说,uni封装的socket没有返回实例对象。你打印的都是undefined。你可以换成JS自带的socket以这种方式接收。
我在uniapp的H5项目是这样连的。你要是多端的话,建议还是用uni封装的。
例如:
// websocket初始化
initWebSocket() {
this.websocket = new WebSocket(this.$Url.ChatWebSocket);
// 连接错误
this.websocket.onerror = this.setErrorMessage;
// 连接成功
this.websocket.onopen = this.setOnopenMessage;
// 收到消息的回调
this.websocket.onmessage = this.screenMsg;
// 连接关闭的回调
this.websocket.onclose = this.setOncloseMessage;
// 监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = this.onbeforeunload;
},