谷歌浏览器出错并且没有建立websocket连接。
与客户端建立websocket连接的服务器日志,并立即断开与消息1006 Connection dropped by remote peer.
服务器代码:
var server = http.createServer(function(req,res) {
...
...
}
server.listen(8000);
sessionmgr.sessiongc();
var cons= []
wsServer = new websockserver({
httpServer: server,
autoAcceptConnections: false
});
wsServer.on('request', function (req) {
//console.log(req);
//accept directly because authenticity of request's origin already established
var con = req.accept(null, req.origin);
cons.push(con);
console.log(cons.length);
console.log('connection accepted req origin = '+req.origin);
con.on('message', function(msg) {
if(msg.type === 'utf8') {
console.log('Message:='+msg.utf8Data);
con.sendUTF('Hello'+" "+msg);
}
else if(msg.type === 'binary') {
console.log('Message:='+msg.binary);
con.sendBytes(msg.binaryData);
}
});
con.on('close',function(reasonCode, description) {
console.log(con.remoteAddress +'disconnected :: '+reasonCode+" "+description);
});
});
客户代码:
<script>
window.addEventListener("load",function(evt) {
var chatbox = document.getElementById("chatbox");
var send = document.getElementById("send");
var chathistory = document.getElementById("chathistory");
var socket = new WebSocket("ws://192.168.23.128:8000/wp",null);
socket.addEventListener("open",function(evt) {
alert(JSON.stringify(evt));
});
socket.addEventListener("message",function(evt) {
chathistory.innerHTML += evt.data;
/*var msgType = jsonMsg['msgType'];
switch(msgType) {
case 'connectionstatus':
updatePlayerConnectionStatus(jsonMsg['payload']);
break;
case 'chatmsg':
updateChat(jsonMsg['payload']);
break;
case 'scoreupdate':
updateScores(jsonMsg['payload']);
break;
}*/
});
send.addEventListener("click",function(evt) {
socket.send(chatbox.value);
chatbox.value = "";
});
});
</script>
使用Firefox时连接不会丢失。当我尝试使用谷歌浏览器进行连接时,它才会丢失。
Chrome控制台错误:
WebSocket connection to 'ws://192.168.23.128:8000/wp' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
但使用Firefox时没有错误。关于如何确保它在Chrome上运行的任何想法?
答案 0 :(得分:0)
将noVNC客户端(二进制websocket)连接到netty websocket服务器时出现了同样的错误。并通过在服务器端设置子协议来解决它:
public class NoVncProxyServer extends WebSocketServer {
public NoVncProxyServer(int port) {
super(port);
}
@Override
protected void initialize(ChannelPipeline pipeline) {
pipeline.addLast(
new HttpServerCodec(),
new HttpObjectAggregator(65536),
/*
Fixed chrome web socket handshake error by setting sub protocols:
WebSocket connection to 'ws://192.168.8.157:35223/websocket' failed:
Error during WebSocket handshake:
Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
*/
new WebSocketServerProtocolHandler("/websocket", "binary"),
new BinaryFrameHandler()
);
}
public static final class BinaryFrameHandler extends
SimpleChannelInboundHandler<BinaryWebSocketFrame> {
static final ChannelActivityHandler activityHandler = new ChannelActivityHandler();
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
activityHandler.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
activityHandler.channelInactive(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
activityHandler.exceptionCaught(ctx, cause);
}
@Override
public void channelRead0(ChannelHandlerContext ctx,
BinaryWebSocketFrame msg) throws Exception {
// Handle binary frame
String frontAddress = HostPort.getToString((InetSocketAddress) ctx.channel().remoteAddress());
//transfer data to target by router
VncProxies.transferRouter.transferTo(frontAddress, msg.content().copy());
}
}
}