我在使用WebSocket nodejs的客户端服务器连接中遇到错误
我正在使用 node-websocket-server https://github.com/miksago/node-websocket-server
错误:
与'ws://127.0.0.1:8000 /'的WebSocket连接失败:WebSocket握手期间出错:'Sec-WebSocket-Accept'标头丢失*
服务器代码(server.js):
var ws = require(__dirname + '/lib/ws/server');
var server = ws.createServer();
server.addListener("connection", function (conn) {
console.log("A connection established with connection id "+ conn.id);
var msg = "Welcome " + conn.id + " joining the party." +
"Total connection : " + server.manager.length;
server.broadcast(msg);
});
server.listen(8000);
console.log("Websocket server is running...");
console.log("Listening to port 8000");
客户代码:
var websock = {};
$(function(){
if(window["WebSocket"]){
websock.socket = new WebSocket("ws://127.0.0.1:8000");
websock.socket.onopen = function(e){
console.log("WebSocket connection established");
};
websock.socket.onmessage = function(e){
console.log(e.data);
};
websock.socket.onclose = function(e){
console.log("WebSocket connection closed.");
};
}
});
index.html :
<!DOCTYPE html>
<html>
<head>
<title>WebSockets</title>
</head>
<body>
<script src="js/jquery-2.1.0.min.js"></script>
<script src="js/client.websocket.js"></script>
</body>
</html>
我不知道这里有什么问题。谢谢。