1、websocket 调用close 方法后,onclose 事件不会马上触发,大概10S 左右才会触发,有时主动调用close 方法 后就不需要触发onclose 事件,可以设置onclose 事件为undefined。
ws.onclose = undefined;
或者如果用 addEventListener 注册的事件,可以使用removeEventListener 移除相关事件。
2、怎么判断onclose 事件是哪个websocket 触发的,可以在onclose 里面拿到currentTarget 对象进行比较。
ws.onclose = function (e)
{
if(e.currentTarget == ws) {
.....
.....
}
}
至于onclose 超时时间,好像并没有找到相关方法可以设置,有朋友知道的麻烦告诉一声。