| | |
| | | |
| | | if (webSocketState.onError) { |
| | | |
| | | var msg = "WebSocket error."; |
| | | var pageProtocol = (typeof window !== 'undefined' && window.location) ? window.location.protocol : 'unknown:'; |
| | | var pageHref = (typeof window !== 'undefined' && window.location) ? window.location.href : 'unknown'; |
| | | var readyState = instance.ws ? instance.ws.readyState : -1; |
| | | var hints = []; |
| | | |
| | | if (pageProtocol === 'https:' && instance.url.indexOf('ws://') === 0) |
| | | hints.push('HTTPS 页面不能连接非安全 ws://,需要改为 wss://'); |
| | | |
| | | if (readyState === 3) |
| | | hints.push('连接在握手阶段已关闭,常见原因:目标端口不是 WebSocket 服务、服务端未处理 Upgrade 握手、端口未开放或反向代理未透传 Upgrade 头'); |
| | | |
| | | var msg = '[JSLIB WebSocket] error url=' + instance.url + |
| | | ' pageProtocol=' + pageProtocol + |
| | | ' readyState=' + readyState + |
| | | ' hasSubProtocols=' + (instance.subprotocols && instance.subprotocols.length > 0 ? 'true' : 'false') + |
| | | ' page=' + pageHref; |
| | | |
| | | if (hints.length > 0) |
| | | msg += ' hints=' + hints.join(' | '); |
| | | |
| | | var length = lengthBytesUTF8(msg) + 1; |
| | | var buffer = _malloc(length); |
| | | stringToUTF8(msg, buffer, length); |
| | |
| | | if (webSocketState.debug) |
| | | console.log("[JSLIB WebSocket] Closed."); |
| | | |
| | | if (webSocketState.onError && ev.code === 1006) { |
| | | var pageProtocol = (typeof window !== 'undefined' && window.location) ? window.location.protocol : 'unknown:'; |
| | | var msg = '[JSLIB WebSocket] abnormal close code=1006 url=' + instance.url + |
| | | ' pageProtocol=' + pageProtocol + |
| | | ' reason=' + (ev.reason || '') + |
| | | ' wasClean=' + (ev.wasClean ? 'true' : 'false') + |
| | | ' hint=通常表示握手失败或网络层直接拒绝;如果当前端口原本是 TCP 游戏服端口,需要单独提供 WebSocket 网关'; |
| | | var length = lengthBytesUTF8(msg) + 1; |
| | | var buffer = _malloc(length); |
| | | stringToUTF8(msg, buffer, length); |
| | | |
| | | try { |
| | | Module.dynCall_vii(webSocketState.onError, instanceId, buffer); |
| | | } finally { |
| | | _free(buffer); |
| | | } |
| | | } |
| | | |
| | | if (webSocketState.onClose) |
| | | Module.dynCall_vii(webSocketState.onClose, instanceId, ev.code); |
| | | |