我正在使用nodejs-ws模块编写一个websocket服务器,但是服务器只能位于服务器的根目录下,所以如何在子主机路由器上进行如localhost:3000 / chat?
我需要你的帮助,非常感谢!
答案 0 :(得分:0)
工作示例:
var ws = require('ws');
var http = require('http');
var httpServer = http.createServer();
httpServer.listen(3000, 'localhost');
var ws1 = new ws.Server({server:httpServer, path:"/chat"});
ws1.on('connection', function(){
console.log("connection on /chat");
});
var ws2 = new ws.Server({server:httpServer, path:"/notifications"});
ws2.on('connection', function(){
console.log("connection on /notifications");
});
答案 1 :(得分:0)
你可以告诉我如何在快递中使用它吗?
使用Express路由websockets而不是使用express-ws-routes
var express = require('express');
var app = require('express-ws-routes')();
app.websocket('/myurl', function(info, cb, next) {
console.log(
'ws req from %s using origin %s',
info.req.originalUrl || info.req.url,
info.origin
);
cb(function(socket) {
socket.send('connected!');
});
});