Websocket failed. Downgrading to Comet and resending
Asked Answered
K

1

7

I am using Atmosphere framework 2.0.0.RC5 to extend my web application with websocket functionality and faced some strange error 'Websocket failed. Downgrading to Comet and resending', which I can't get away.

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

The application has html+js client and a java backend.

Backend

  • Tomcat 7.0.42 with NIO protocol enabled
  • Web module v3.0 with Spring and Atmosphere servlets
  • Custom CORS filter to allow atmosphere headers
  • Every received message is logged by the server

(extra 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 a 'index.html' i see an error in the console (jquery.atmosphere-2.0.2.js line 1097) and no messages in server log:

Uncaught TypeError: Cannot set property 'webSocketOpened' of null

Surprisingly it works when I type directly in the console:

Atmosphere connected using websocket

And gets logged by the server, so I assume the server part is fine:

Logged by server

My guess it's a javascript issue, but I'm not 100% sure. I played with connectTimeout parameter with no luck. Any ideas why it does not work in the script?

Kahle answered 12/9, 2013 at 21:1 Comment(3)
transport: "websocket",, you have 2x ,,Nummary
typo, sorry, corrected.Kahle
Maybe try to use the default annotation and see if it works?var request = new $.atmosphere.AtmosphereRequest(); request.url = 'http://localhost:8181/services/echo'; etc..Nummary
K
0

Move the channel.push code to inside the onOpen function.

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

Kirstiekirstin answered 21/1, 2014 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.