本文介绍了在Firefox中的WebSocket建立两个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个WebSocket服务器。当我使用WebSocket连接到服务器在Firefox中,我发现两个连接建立,其中一个从来没有发送任何数据...

我的firefox版本是15.0.1

在Chrome中运行相同的代码是可以的,连接一次,只建立一个连接。

有没有人有这样的麻烦?



有服务器的代码:

  ServerSocket svrSock = new ServerSocket(); 
svrSock.bind(new InetSocketAddress(0.0.0.0,11111));
while(true){
try {
// accept connection
Socket clientSock = svrSock.accept();
//打印连接到这个服务器的套接字
System.out.println(accept socket:+ clientSock);

//为客户端运行线程
new ClientThread(clientSock).start();
} catch(Exception e){
e.printStackTrace();


$ / code $ / pre

还有就是js代码:

  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!');
};

当我在firefox中运行这个js代码时,我在服务器控制台中得到了这个:


$ b


解决方案

是Firefox 15中的一个问题,它将会在firefox 16中修复:

Firefox 15正在进行一个推测连接,它可以很好地处理HTTP / SPDY,但是因为WebSocket握手是HTTP 1.0(而不是1.1),它不能重新使用推测连接,并必须进行第二次连接。

如果您的服务器不是关键问题是正确的多线程,可以接受多个连接,但它是讨厌的。


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:

解决方案

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.

这篇关于在Firefox中的WebSocket建立两个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:05
查看更多