webSocket在游戏web游戏开发中经常会被使用到,然而在微信浏览器中,经常在点击按钮之后,进行WebSocket连接,会引发崩溃现象。这种情况的解决方案是setTimeout
即
var socket = new WebSocket(url);
socket.binaryType = 'arraybuffer';
socket.onopen = onopen;
socket.onmessage = onmessage;
socket.onerror = onerror;
socket.onclose = onclose;
如果引发崩溃,那么,使用setTimeout
,将执行内容放入,即可解决崩溃问题
setTimeout(function() {
var socket = new WebSocket(url);
socket.binaryType = 'arraybuffer';
socket.onopen = onopen;
socket.onmessage = onmessage;
socket.onerror = onerror;
socket.onclose = onclose;
}.bind(this), 0);