我需要在onClose()事件上添加自动重新连接(过一会儿。)。
我将标准的websocket对象包装在自定义的类/函数中:
function IoWebSocket( url, port, mode) {
this.url = url;
this.isOpen = false;
...
this.wsOpen = function() {
ws = new WebSocket( ... );
try {
ws.onopen = function() {
console.info("onOpen");
..
}
..
ws.onclose = function(){
this.isOpen = false;
console.info("onClose");
setTimeout(function() {
console.info("Reconnecting ..");
wsOpen();
}, 10000);
}
} catch(exception) {
alert('Error: ' + exception);
}
}
碰巧调用了onclose回调,但是浏览器引发了错误:
ReferenceError: wsOpen is not defined at ..
我也尝试使用this.wsOpen()
,但收到错误消息:
TypeError: this.wsOpen is not a function at ..
如何重新连接服务器?