当首选传输失败时,大气试图使用回退传输。它试图重新连接maxReconnect时代。之后,它调用了onClose和onError。
当我再次尝试订阅大气层时,总是使用连接的回退传输。
以下是我的客户端配置:
AtmosphereRequestConfig jsonRequestConfig = // ...
jsonRequestConfig.setTransport(AtmosphereRequestConfig.Transport.WEBSOCKET);
jsonRequestConfig.setFallbackTransport(AtmosphereRequestConfig.Transport.LONG_POLLING);
jsonRequestConfig.setLogLevel("debug");
jsonRequestConfig.setMaxReconnectOnClose(1);
Atmosphere atmosphere = Atmosphere.create();
clientRequest = atmosphere.subscribe(jsonRequestConfig);当我的服务器正在运行,客户端第一次连接(页面重新加载)时,连接就在Websockets上。然后我停止了我的服务器,客户端显示如下:
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: websocket.onclose
atmosphere.js:3252 Websocket closed, reason: Normal closure; the connection successfully completed whatever purpose for which it was created. - wasClean: true
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Firing onClose (closed case)
ConsoleLogger.java:32 AtmosphereListener: onClose
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: invoking .close() on WebSocket object
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Firing onReconnect
ConsoleLogger.java:32 AtmosphereListener: onReconnect
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: Firing onReconnect
atmosphere.js:3252 Invoking executeWebSocket, using URL: ws://www.example.com:8080/socket/widget/2418C3F0-9A9B-48C4-8EE3-0541465EEACD%7C%7Crefdgdfgwe324234%7C%7CAF8F7A67-CDD0-4AD3-96C4-D447E970D0F8?X-Atmosphere-tracking-id=f89f00d8-b412-4825-80f9-ac8ca280edb5&X-Atmosphere-Framework=2.3.1-javascript&X-Atmosphere-Transport=websocket&Content-Type=application/json; charset=UTF-8&X-atmo-protocol=true
atmosphere.js:3252 Sat Jan 30 2016 22:17:14 GMT+0100 (CET) Atmosphere: websocket.onopen
atmosphere.js:3252 Websocket successfully opened
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: websocket.onclose
atmosphere.js:3252 Websocket closed, reason: Connection was closed abnormally (that is, with no close frame being sent). - wasClean: false
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: invoking .close() on WebSocket object
atmosphere.js:3252 Websocket reconnect maximum try reached 2
atmosphere.js:3252 Websocket error, reason:
atmosphere.js:3252 Sat Jan 30 2016 22:17:15 GMT+0100 (CET) Atmosphere: Firing onError, reasonPhrase: maxReconnectOnClose reached然后我重新启动我的服务器。客户端自动重新连接,因为他试图在onError中插入气氛连接。这是我得到的日志:
atmosphere.js:1195 WebSocket connection to 'ws://www.example.com:8080/socket/widget/2418C3F0-9A9B-48C4-8EE3-0541465EEACD%7C%7Crefdgdfgwe324234%7C%7CAF8F7A67-CDD0-4AD3-96C4-D447E970D0F8?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=2.3.1-javascript&X-Atmosphere-Transport=websocket&Content-Type=application/json;%20charset=UTF-8&X-atmo-protocol=true' failed: Error during WebSocket handshake: Unexpected response code: 302
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: websocket.onerror
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: websocket.onclose
atmosphere.js:3252 Websocket closed, reason: Connection was closed abnormally (that is, with no close frame being sent). - wasClean: false
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: Firing onClose (closed case)
ConsoleLogger.java:32 AtmosphereListener: onClose
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: Request already closed, not firing onClose (closed case)
atmosphere.js:3252 Websocket failed on first connection attempt. Downgrading to long-polling and resending
ConsoleLogger.java:32 AtmosphereListener: onTransportFailure: Websocket failed on first connection attempt. Downgrading to long-polling and resending
ConsoleLogger.java:32 AtmosphereListener: onTransportFailure: Websocket failed on first connection attempt. Downgrading to long-polling and resendingmhi_g$ @ ConsoleLogger.java:32Hgi_g$ @ SimpleConsoleLogHandler.java:36cgi_g$ @ Logger.java:262bgi_g$ @ Logger.java:250ugi_g$ @ Logger.java:178tgi_g$ @ Logger.java:162Agi_g$ @ Logger.java:129pNi_g$ @ AtmosphereListener.java:114(anonymous function) @ AtmosphereRequestConfig.java:389shc_g$ @ Impl.java:239vhc_g$ @ Impl.java:291(anonymous function) @ Impl.java:77_reconnectWithFallbackTransport @ atmosphere.js:1729_websocket.onclose @ atmosphere.js:1534
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 2
2atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 3
atmosphere.js:2117 XHR finished loading: GET "http://www.example.com:8080/socket/widget/2418C3F0-9A9B-48C4-8…plication%2Fjson%3B%20charset%3DUTF-8&X-atmo-protocol=true&_=1454188963647"._executeRequest @ atmosphere.js:2117_execute @ atmosphere.js:644_reconnectWithFallbackTransport @ atmosphere.js:1745_websocket.onclose @ atmosphere.js:1534
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 4
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: ajaxRequest.onreadystatechange, new state: 2
atmosphere.js:3252 Sat Jan 30 2016 22:22:43 GMT+0100 (CET) Atmosphere: Firing onOpen日志上写着:
第一次连接尝试时Websocket失败。降级为长轮询和重发
如何在服务器重新启动时强制Atmosphere.js使用首选传输(Websockets)而不是后备传输(长轮询)?
发布于 2016-02-06 21:29:45
如何在服务器重新启动时强制Atmosphere.js使用首选传输(Websockets)而不是后备传输(长轮询)?
大气客户端自动尝试使用fallbackTransport重新连接。这是默认行为。
若要强制它使用首选传输重新连接,您可能必须更改默认行为。
可以通过在atmosphere.js 请看这里中注释掉这一行来改变这一点。
这将确保在重新连接时始终使用首选的web套接字传输,并且不会使用fallbackTransport。
_websocket.onclose =函数(WebSocketOpened){//.if (_abortingConnection) { atmosphere.util.log(_request.logLevel,"Websocket关闭正常“);如果您永远不想使用回退传输},则/** remove,否则如果(!webSocketOpened) { _reconnectWithFallbackTransport("Websocket在第一次连接尝试时失败”)。*/ } if (_request.reconnect & _response.transport === 'websocket‘){ _clearState();if (_requestCount++ < _request.maxReconnectOnClose) {_open(’重新连接‘,_request.transport,_request);若(_request.reconnectInterval > 0) { _request.reconnectId =setTimeout(函数() { _response.responseBody = "";_response.messages = [];_executeWebSocket(真);},_request.reconnectInterval);_response.messages = [];_executeWebSocket(真);}}atmosphere.util.log{ atmosphere.util.log(_request.logLevel,"Websocket重新连接最大尝试达到“+ _requestCount);if (_canLog(‘警告’)){ atmosphere.util.warn("Websocket错误,原因:”+ message.reason);} _onError(0,"maxReconnectOnClose到达“);}
另一种方法是利用onReconnect回调动态切换clientRequest.request参数。
为什么?
因为当连接关闭并调用onTransportFailure时。大气将传输更改为fallbackTransport,并最终可能导致'none‘(如果它被设置为websocket)。
假设您的请求包括以下配置:
request.reconnectOnServerError=真,request.executeCallbackBeforeReconnect=真;
您可以将其添加到onReconnect回调中:
clientRequest.request.transport = 'websocket'; // force preferred transport on reconnect //clientRequest.request.fallbackTransport = 'websocket'; // optional
问题是,如果您将回退设置为websocket,它只会尝试重新连接一次,就像将其保留为空白一样,如果您使用的是url(从ws://或wss://开始)而不是webSocketUrl,它仍然将使用web套接字协议。
request.url = 'ws://example.com/ws';
https://stackoverflow.com/questions/35057229
复制相似问题