2016-11-06 16 views
2

私はnodejs-wsモジュールを使ってWebSocketサーバーを書いていますが、サーバーはサーバーのルートにしか置かないので、localhost:3000/chatのような子ルーターでどのようにすることができますか?websocketサーバーをルーターにする方法は?

私はあなたの助けが必要です、ありがとう!

答えて

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"); 
}); 
+0

おかげで多くを使用したい、とあなたはどのように急行でこれを使用する方法を教えてくださいだろうか? – laoqiren

0

あなたはどのように速達でこれを使用する方法を教えてくださいだろうか? ExpressでルートWebSocketをするには

私はむしろ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!'); 
    }); 
}); 
関連する問題