1

I've got a Java web application deployed on a local GlassFish 4.1 server that implements WebSockets to inter-operate with the web client. I'm able to successfully execute client-to-server communication over the socket, but server-to-client communication doesn't work for some reason.

The Java code that sends messages to the client:

try 
{
    String msg = ServerClientInteropManager.toResponseJSON(response);
    parentSession.getBasicRemote().sendText(msg);
    FLAIRLogger.get().info("Sent response to client. Message: " + msg);
}
catch (IOException ex) {
    FLAIRLogger.get().error("Couldn't send message to session " + parentSession.getid() + ". Exception - " + ex.getMessage());
}

The Javascript code:

pipeline_internal_onMessage = function(event)
{
    var msg = JSON.parse(event.data);
    console.log("Received message from server. Data: " + event.data);
};

function pipeline_init()
{
    if (PIPELINE !== null || PIPELINE_CONNECTED === true)
    {
        console.log("Pipline already initialized");
        return false;
    }
    else 
    {
        var pipelineURI = "ws://" + document.location.host + document.location.pathname + "webranker";
        console.log("Attempting to establish connection with WebSocket @ " + pipelineURI);

        if ('WebSocket' in window)
            PIPELINE = new WebSocket(pipelineURI);
        else if ('MozWebSocket' in window)
            PIPELINE = new MozWebSocket(pipelineURI);
        else
        {
            console.log("FATAL: No WebSockets support");
            alert("This browser does not support WebSockets. Please upgrade to a newer version or switch to a browser that supports WebSockets.");
            return false;
        }

        // the other event listeners get added here
        PIPELINE.onMessage = pipeline_internal_onMessage;
        PIPELINE_CONNECTED = true;

        window.onbeforeunload = function() {
            pipeline_deinit();  
        };

        console.log("Pipeline initialized");
        return true;
    }
}

The onMessage function is never fired, even when the server successfully calls the sendText() method. Using the AsyncRemote yields the same results. The onError listeners on both ends don't report anything either. This is my first time working with sockets so I might be missing something elementary.

1 Answer 1

1

replace

PIPELINE.onMessage = pipeline_internal_onMessage

with

PIPELINE.onmessage = pipeline_internal_onMessage

Please refer here for more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.