Website crash. Transition to comet and retransmission

I am using the Atmosphere 2.0.0.RC5 environment to extend my web application using websocket functions and I encountered some strange error "Websocket failed. Transition to comet and redirect", which I cannot leave.

I used the websocket chat example as a starting point: https://github.com/Atmosphere/atmosphere-samples/tree/master/samples/websocket-chat

The application has an html + js client and a java server.

Backend

  • Tomcat 7.0.42 with NIO enabled
  • Web Module v3.0 with Spring and Atmosphere Servlets
  • Custom CORS filter for creating atmosphere headers.
  • Each received message is logged by the server.

(optional code omitted)

public void onTextMessage(WebSocket webSocket, String message) throws IOException { logger.info("Message received: " + message); webSocket.broadcast(mapper.writeValueAsString(mapper.readValue(message, Data.class))); } 

Client

index.html

 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="js/lib/jquery.atmosphere-2.0.2.js"></script> <script type="text/javascript" src="js/aimpl.js"></script> 

aimpl.js

 $(function() { "use strict"; var socket = $.atmosphere; var request = { url: 'http://localhost:8181/services/echo', transport: "websocket", contentType: "application/json", //connectTimeout: '300000', fallbackTransport: 'long-polling', enableXDR: true, logLevel: 'debug' }; request.onMessage = function (response) { console.log(response.responseBody) }; request.onOpen = function(response) { console.log('Atmosphere connected using ' + response.transport); }; request.onReconnect = function (request, response) { console.log("reconnecting..."); }; request.onError = function(response) { console.log('an error has occured.'); }; var channel = socket.subscribe(request); channel.push(JSON.stringify({ author: 'me', message: 'hello from websocket!' })); }); 

When I open "index.html", I see an error in the console (jquery.atmosphere-2.0.2.js 1097 line) and messages in the server log:

Uncaught TypeError: Cannot set property 'webSocketOpened' of null

Surprisingly, this works when I type directly in the console:

Atmosphere connected using websocket

And it registers with the server, so I assume the part of the server is beautiful:

Logged by server

I assume this is a javascript issue, but I'm not 100% sure. I played with the connectTimeout parameter with no luck. Any ideas why this is not working in the script?

+6
source share
1 answer

Move the channel.push code inside the onOpen function.

The error message is the result of trying to send data before the connection is established. More here

0
source

Source: https://habr.com/ru/post/953731/


All Articles