我正在用 Java 编写 WebSocket 服务器。当我在firefox中使用WebSocket连接服务器时,我发现有两个连接建立了,其中一个从来没有发送任何数据...
我的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();
// 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();
}
}
还有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 代码时,我在我的服务器控制台中得到了这个:
接受套接字:Socket[addr=/127.0.0.1,port=56935,localport=11111]
接受套接字:Socket[addr=/127.0.0.1,port=56936,localport=11111]