2
votes

I'm newbie to the web-socket programming...

I have the following JavaScript client code:

var connection = new WebSocket('ws://localhost:8080/OmegaThings/registerdevice');

        connection.onopen = function () {              
            console.log("Socket has been opened state = " + connection.readyState);
            connection.send('Ping'); // Send the message 'Ping' to the server             
            connection.send('Websocket client');
        };

        console.log("Socket has been opened state = " + connection.readyState);
        connection.send('finish');

        // Log errors
        connection.onerror = function (error) {
        console.log('WebSocket Error ' + error);
        };

        // Log messages from the server
        connection.onmessage = function (e) {
        console.log('Server: ' + e.data);
        };   

Java endpoint:

@ServerEndpoint("/registerdevice") 
public class RegisterDeviceEndPoint 
{
    private static final Logger LOG = Logger.getLogger(RegisterDeviceEndPoint.class.getName());

    @OnOpen  
    public void connectionOpened() 
    {    
        LOG.log(Level.INFO, "******************connection opened**************");
    }

    @OnMessage  
    public synchronized void processMessage(Session session, String message) 
    {    
        LOG.log(Level.INFO, "received message: {0}", message);
    }

   @OnClose  
   public void connectionClosed() 
   {    
       LOG.log(Level.INFO, "connection closed");  
   }
}

on the firefox console I got the following output:

"Socket has been opened state = 1"
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
"Socket has been opened state = 0"

on the GlassFish server log I got "ping" and "Websocket client", but the connection closed after onopen event exit(not sure), thus, the last word "finish" doesn't appear on the log and the error occurs.

I want to know if my code is correct?
What causes the error? javascript code, GlassFish server configuration or the java endpoint code?

1
I think u refreshed the page that might terminate the existing connection. may be this link will help u stackoverflow.com/questions/21675247/… - mallikarjun

1 Answers

-1
votes

Try to change the glassfish 8080 port, eg: 8887, or make sure Your antivirus/other application are not using port 80, I previously had experience where my server websocket was blocked by antivirus which using port 80.