ws = new WebSocket(‘ws://192.168.150.53:8080/websocket’);
ws.send(“111”);
因为ws连接还没有建立成功,不能在new完后直接跟着send。
js代码:
<script>
var interval = 1000;
var send = function (ws,message, callback){
waitForConnection(ws,function(){
ws.send(message);
if(typeof callback!=='undefined'){
callback();
}
},interval);
};
var waitForConnection = function(ws,callback,interval){
if(ws.readyState === 1){
callback();
} else {
//可选:实现间隔在这里
setTimeout(function(){
waitForConnection(ws,callback,interval);
},interval);
}
};
</script>
// 阻塞等待ws连接状态完成
send(ws,messageJson,function(){
console.log("send callback");
$("#log-info div").empty();
$("#log-info div").append("正在查询...").css("color", "#FFFFFF");
})
参考
https://www.it1352.com/743149.html