webSocket异常关闭
发布于 6 年前 作者 txia 15393 次浏览 来自 问答

var http = require("…/…/utils/util.js")

Page({

data: {

socketTask: null,

allContentList: []

},

onLoad: function() {},

openBtnClick: function() {

let that = this;

let socketTask = wx.connectSocket({

url: ‘wss://cepingtest.nnyun.net/chat’,

header: {

‘content-type’: ‘application/x-www-form-urlencoded’,

‘channel’: ‘Xn4qDGinJJkSs8xWhJU’

},

success: function(res) {

console.log(“建立连接成功”);

console.log(res);

},

fail: function(res) {

console.log(“建立连接失败”);

console.log(res);

}

});

socketTask.onOpen(function(res) {

console.log(“连接打开”);

console.log(res);

});

socketTask.onClose(function(res) {

console.log(“连接关闭”);

console.log(res);

});

socketTask.onError(function(res) {

console.log(“连接错误”);

console.log(res);

});

socketTask.onMessage(function(res) {

console.log(“服务端返回消息”);

var DataJson = JSON.parse(res.data)

var temps = JSON.parse(DataJson.contentJson);

var temp = temps.data

console.log(temp)

that.data.allContentList.push(temp);

that.setData({

allContentList: that.data.allContentList,

})

});

this.data.socketTask = socketTask;

},

closeBtnClick: function() {

let socketTask = this.data.socketTask;

if (socketTask) {

socketTask.close({

success: function(res) {

console.log(“关闭接口成功”);

console.log(res);

},

fail: function(res) {

console.log(“关闭接口失败”);

console.log(res);

}

});

}

},

sendBtnClick: function() {

console.log(“尝试发送消息:”);

var data = {

content: ‘你好’,

contentType: 1,

contentValue: ‘’,

sendTime: http.formatTime(new Date()),

userId: ‘CrCubJtbShINpbPYPSg’,

roomId: ‘Xn4qDGinJJkSs8xWhJU’,

evaluateMode: 2

}

var msg = {

“name”: “evaluation.addcontent”,

“version”: 0,

“happenTime”: Date.parse(new Date()),

“contentJson”: JSON.stringify(data),

“inputChannelEnum”: “WEBSOCKET”

}

let socketTask = this.data.socketTask;

if (this.data.socketTask) {

socketTask.send({

data: JSON.stringify(msg),

success: function(res) {

console.log(“发送消息成功”);

console.log(res);

},

fail: function(res) {

console.log(“发送消息失败”);

console.log(res);

}

});

}

}

})

打开socket连接之后,点击发送消息,然后关闭连接,等待socketTask.onClose()事件,发现返回的是下面的情况

这是后端的原因还是前端的问题呢

代码片段是wechatide://minicode/zXXbKomL7Z3R

3 回复

试了一下 ,应该是服务器没有响应中止连接请求

可以检查下服务器的逻辑

,这边测试是没问题的

回到顶部