websocket 适用于 http 模块但不适用于 express (node.js)
websocket works with http module but not with express (node.js)
我正在尝试使用 express 和 websocket 模块使 websokects 与 node.js 一起工作。
有趣的是,如果我使用 http 模块创建服务器,它们会按预期工作,我会收到状态 101。
但是如果我使用 express 模块创建服务器,它会抛出一个错误:
WebSocket connection to 'ws://localhost:2345/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
这是代码
// using http module (works)
var WebSocketServer = require("websocket").server;
var http = require("http");
var app = http.createServer(function(request, response) {
response.writeHeader(200, {'Content-Type':'text/html'});
response.end(""+
"<!DOCTYPE html>"+
"<html>"+
"<head>"+
"<script>"+
"var ws = new WebSocket('ws://localhost:2345/');"+
"ws.onmessage = function(event) { "+
"var span = document.createElement('span');"+
"span.innerHTML = event.data;"+
"document.body.appendChild(span);"+
"}"+
"</script>"+
"</head>"+
"<body>"+
"<span>Messages: </span>"+
"</body>"+
"</html>"
);
});
app.listen(2345);
wsServer = new WebSocketServer({'httpServer':app});
wsServer.on("request", function(request) {
var connection = request.accept(null, request.origin);
console.log("Connection ACCEPTED\n");
connection.on("message", function(message)
{
if(message.type == 'utf8')
{
console.log("Received Message: %s", message.utf8Data);
connection.sendUTF(message.utf8Data);
}
})
connection.on("close", function(reasonCode, description)
{
console.log("Connection lost\n");
})
})
和非工作部分
// using express module (get error)
var WebSocketServer = require("websocket").server;
var app = require('express')();
var app.get('/', function(request, response) {
response.writeHeader(200, {'Content-Type':'text/html'});
response.end(""+
"<!DOCTYPE html>"+
"<html>"+
"<head>"+
"<script>"+
"var ws = new WebSocket('ws://localhost:2345/');"+
"ws.onmessage = function(event) { "+
"var span = document.createElement('span');"+
"span.innerHTML = event.data;"+
"document.body.appendChild(span);"+
"}"+
"</script>"+
"</head>"+
"<body>"+
"<span>Messages: </span>"+
"</body>"+
"</html>"
);
});
app.listen(2345);
wsServer = new WebSocketServer({'httpServer':app});
wsServer.on("request", function(request) {
var connection = request.accept(null, request.origin);
console.log("Connection ACCEPTED\n");
connection.on("message", function(message)
{
if(message.type == 'utf8')
{
console.log("Received Message: %s", message.utf8Data);
connection.sendUTF(message.utf8Data);
}
})
connection.on("close", function(reasonCode, description)
{
console.log("Connection lost\n");
})
})
哪里出了问题?如何解决这个问题?
Express 中的 app
不是 httpServer
,因此如果 WebSocketServer
不明确了解 Express 以及如何从中获取服务器,那么您可能需要给它实际的 httpServer
对象而不是 Express
对象,你可以通过改变这个来做到:
app.listen(2345);
wsServer = new WebSocketServer({'httpServer':app});
对此:
var server = app.listen(2345);
var wsServer = new WebSocketServer({'httpServer': server});
我正在尝试使用 express 和 websocket 模块使 websokects 与 node.js 一起工作。
有趣的是,如果我使用 http 模块创建服务器,它们会按预期工作,我会收到状态 101。
但是如果我使用 express 模块创建服务器,它会抛出一个错误:
WebSocket connection to 'ws://localhost:2345/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
这是代码
// using http module (works)
var WebSocketServer = require("websocket").server;
var http = require("http");
var app = http.createServer(function(request, response) {
response.writeHeader(200, {'Content-Type':'text/html'});
response.end(""+
"<!DOCTYPE html>"+
"<html>"+
"<head>"+
"<script>"+
"var ws = new WebSocket('ws://localhost:2345/');"+
"ws.onmessage = function(event) { "+
"var span = document.createElement('span');"+
"span.innerHTML = event.data;"+
"document.body.appendChild(span);"+
"}"+
"</script>"+
"</head>"+
"<body>"+
"<span>Messages: </span>"+
"</body>"+
"</html>"
);
});
app.listen(2345);
wsServer = new WebSocketServer({'httpServer':app});
wsServer.on("request", function(request) {
var connection = request.accept(null, request.origin);
console.log("Connection ACCEPTED\n");
connection.on("message", function(message)
{
if(message.type == 'utf8')
{
console.log("Received Message: %s", message.utf8Data);
connection.sendUTF(message.utf8Data);
}
})
connection.on("close", function(reasonCode, description)
{
console.log("Connection lost\n");
})
})
和非工作部分
// using express module (get error)
var WebSocketServer = require("websocket").server;
var app = require('express')();
var app.get('/', function(request, response) {
response.writeHeader(200, {'Content-Type':'text/html'});
response.end(""+
"<!DOCTYPE html>"+
"<html>"+
"<head>"+
"<script>"+
"var ws = new WebSocket('ws://localhost:2345/');"+
"ws.onmessage = function(event) { "+
"var span = document.createElement('span');"+
"span.innerHTML = event.data;"+
"document.body.appendChild(span);"+
"}"+
"</script>"+
"</head>"+
"<body>"+
"<span>Messages: </span>"+
"</body>"+
"</html>"
);
});
app.listen(2345);
wsServer = new WebSocketServer({'httpServer':app});
wsServer.on("request", function(request) {
var connection = request.accept(null, request.origin);
console.log("Connection ACCEPTED\n");
connection.on("message", function(message)
{
if(message.type == 'utf8')
{
console.log("Received Message: %s", message.utf8Data);
connection.sendUTF(message.utf8Data);
}
})
connection.on("close", function(reasonCode, description)
{
console.log("Connection lost\n");
})
})
哪里出了问题?如何解决这个问题?
Express 中的 app
不是 httpServer
,因此如果 WebSocketServer
不明确了解 Express 以及如何从中获取服务器,那么您可能需要给它实际的 httpServer
对象而不是 Express
对象,你可以通过改变这个来做到:
app.listen(2345);
wsServer = new WebSocketServer({'httpServer':app});
对此:
var server = app.listen(2345);
var wsServer = new WebSocketServer({'httpServer': server});