一尘不染

如何在Express中为Websocket服务器设置路由?

node.js

我有一个与此类似的设置:

var WebSocketServer = require("ws").Server,
    express = require("express"),
    http = require("http"),
    app = express(),
    server = http.createServer(app);

app.post("/login", login);
app.get("/...", callSomething);
// ...

server.listen(8000);


var wss = new WebSocketServer({server: server});

wss.on("connection", function(ws){
   // ...
});

我想将WebSocketServer放在特定的路径下,例如"...com/whatever"。问题是如何设置路径?可能吗?


阅读 275

收藏
2020-07-07

共1个答案

一尘不染

您将要使用以下path选项:

var wss = new WebSocketServer({server: server, path: "/hereIsWS"});

在此处查看完整文档

2020-07-07