后台要配置一个ws://的域名
uni.connectSocket({
url: 'ws://120.25.249.130:8001/socketText/1'
});
uni.onSocketOpen(function(res) {
socketOpen = true;
for (var i = 0; i < socketMsgQueue.length; i++) {
sendSocketMessage(socketMsgQueue[i]);
}
socketMsgQueue = [];
});
uni.onSocketError(function(res) {
console.log('WebSocket连接打开失败,请检查!');
});
function sendSocketMessage(msg) {
if (socketOpen) {
uni.sendSocketMessage({
data: msg
});
} else {
socketMsgQueue.push(msg);
}
}
uni.onSocketMessage(function(res) {
console.log('收到服务器内容:' + res.data);
});