清都飘渺客 2022-02-28 14:53 采纳率: 100%
浏览 1049
已结题

关于#websocket#的问题:可以正常连接,但是只要发送消息就报错1002

weixin_33670786 报错信息:
websocket 断开: 1002 An unrecoverable IOException occurred so the connection was closed

错误描述:
可以正常连接,但是只要发送消息(无论是心跳消息还是正常消息),都会断开连接

补充:
接口在websocket检测工具上,是可以正常收发消息的。但是运行到项目中就会报错

代码如下:

initWebSocket() {
                var url = "wss://www.xxx/" + this.userId;
                if ('WebSocket' in window) {
                    this.ws = new WebSocket(url);
                } else if ('MozWebSocket' in window) {
                    this.ws = new MozWebSocket(url);
                }
                this.ws.onmessage = this.onmessage;
                this.ws.onopen = this.onopen;
                this.ws.onclose = this.onclose;
                this.ws.onerror = this.onerror;
            },
            reconnect() { //重新连接
                var that = this;
                if (that.lockReconnect) {
                    return;
                };
                that.lockReconnect = true;
                //没连接上会一直重连,设置延迟避免请求过多
                that.timeoutnum && clearTimeout(that.timeoutnum);
                that.timeoutnum = setTimeout(function() {
                    //新连接
                    that.initWebSocket();
                    that.lockReconnect = false;
                }, 5000);
            },
            reset() { //重置心跳
                var that = this;
                //清除时间
                clearTimeout(that.timeoutObj);
                clearTimeout(that.serverTimeoutObj);
                //重启心跳
                that.start();
            },
            start() { //开启心跳
                console.log('开启心跳');
                var self = this;
                self.timeoutObj && clearTimeout(self.timeoutObj);
                self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj);
                self.timeoutObj = setTimeout(function() {
                    if (self.ws.readyState == 1) { //如果连接正常
                        let params = {
                            To: self.userId,
                            message: "ping..."
                        };
                        self.ws.send(params) //这里发送一个心跳,后端收到后,返回一个心跳消息
                    } else { //否则重连
                        self.reconnect()
                    }
                    self.serverTimeoutObj = setTimeout(function() {
                        self.ws.close() //超时关闭
                    }, self.timeout)
                }, self.timeout)
            },
            onopen() {
                console.log("open")
                this.start() //开启心跳
            },
            onmessage(e) {
                if (e.data.message == 'ping...') {
                    console.info('接收到心跳消息') // 心跳消息,不作处理
                } else {
                    const redata = e.data
                    if (redata.messageType == '加钟') {
                        this.addClockOrderList.push(redata.orderSn)
                        if (this.addClockOrderList.length > 0) {
                            this.matchingList()
                        }
                    }
                }
                this.reset(); //收到服务器信息,心跳重置
            },
            onclose(e) {
                console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
                this.reconnect() //重连
            },
            onerror(e) {
                console.log("出现错误")
                this.reconnect() //重连
            },
            onsend(msg) {
                this.ws.send(msg) //数据发送
            },

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-02-28 15:26
    关注

    原生websocket无法直接send对象,要JSON.stringify转json字符串后发送,要不直接发送对象调用默认的toString方法,导致发送的是 [object Object] 这些内容,数据出错了

    self.ws.send(params)
    ==>
    self.ws.send(JSON.stringify(params) )

    代码最后还有依据send,不知道msg参数是类型,对象也要调用JSON.stringify转json字符串后在发送

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月8日
  • 已采纳回答 2月28日
  • 修改了问题 2月28日
  • 创建了问题 2月28日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵