Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket in Firefox establish two connection

I'm coding a WebSocket server in Java. When I use WebSocket to connect to the server in firefox, I found two connection were established, and one of them never send any data...
My firefox version is 15.0.1
The same code run in Chrome is OK, connect once, established only one connection.
Does anybody have the trouble like this?

There is the server's code:

ServerSocket svrSock = new ServerSocket();
svrSock.bind(new InetSocketAddress("0.0.0.0", 11111));
while(true) {
    try {
        // accept connection
        Socket clientSock = svrSock.accept();
        // print the socket which connected to this server
        System.out.println("accept socket: " + clientSock);

        // run a thread for client
        new ClientThread(clientSock).start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And there is the js code:

var url = 'ws://localhost:11111/test/';
var ws = new WebSocket(url);
ws.onopen = function(){
    console.log('connected!');
    ws.send(11111);
    ws.close();
};
ws.onclose = function(){
    console.log('closed!');
};

When I run this js code in firefox, I get this in my server console:

accept socket: Socket[addr=/127.0.0.1,port=56935,localport=11111]
accept socket: Socket[addr=/127.0.0.1,port=56936,localport=11111]

like image 672
takamachi Avatar asked Oct 07 '22 14:10

takamachi


1 Answers

This is a problem in Firefox 15 that is/will be fixed in firefox 16: https://bugzilla.mozilla.org/show_bug.cgi?id=789018

Firefox 15 is doing a speculative connect which is fine with HTTP/SPDY but because the WebSocket handshake is HTTP 1.0 (rather than 1.1) it is not able to re-use the speculative connection and has to make a second connection.

It's not a critical issue if your server is properly multithreaded and can accept multiple connections but it is annoying.

like image 134
kanaka Avatar answered Oct 10 '22 01:10

kanaka