Websocket 端点和 Express 路由器
我试图在我的 Express 应用程序中实现 Web 套接字(npm ws),但陷入了如何实现我的 Web 套接字以便它们与 Express 路由器一起工作的问题。
目前,我的端点看起来像这样...
router.post('/create-note', jwtAuthentication, NotesController.createNote);
router.get('/get-notes/:id',jwtAuthentication, NotesController.getUserNotes);
router.??('/socket-endpoint', NotesController.wssNote);
如您所见,我不确定在路由器上调用什么方法。我尝试过使用“get”和“post”,但由于某种原因,它只有在我在邮递员上尝试第二个连接后才有效。 (我单击“连接”,没有任何反应,然后单击“断开连接”并再次连接,它就可以工作了。)
我知道我可以在创建 WebSocketServer 时传入路径...
var wss = new WebSocketServer({server: server, path: "/hereIsWS"});
这确实有效,但是如果可以将路由器与 web 一起使用-sockets,我认为这会让我的整个项目变得更加干净和更有组织。
我遇到过有人推荐“express-ws”,但想知道是否有更好的方法来解决我的问题,特别是不涉及其他软件包的方法。
提前致谢!
I was trying to implement web-sockets (npm ws) in my express application, but got stuck on how I should implement my websockets so that they work with express router.
Currently, my endpoints look like this...
router.post('/create-note', jwtAuthentication, NotesController.createNote);
router.get('/get-notes/:id',jwtAuthentication, NotesController.getUserNotes);
router.??('/socket-endpoint', NotesController.wssNote);
As you can see, I am unsure of what method to call on my router. I have tried using 'get' and 'post, but for some reason it only works after I try a second connection on postman. (I click connect, nothing happens, I then click disconnect and connect again and it works.)
I know that I can pass in the path when creating the WebSocketServer...
var wss = new WebSocketServer({server: server, path: "/hereIsWS"});
This does work, but if it is possible to use routers with web-sockets, I think it would make my whole project much cleaner and more organised.
I have come across people recommending 'express-ws', but was wondering if there was a better method to solve my problem, specifically a method that does not involve other packages.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不将 Express 路由器与 webSocket 一起使用。这不是 webSocket 的正确架构。您的 webSocket 服务器可以与 Express 共享一个 http 服务器,但这几乎是两者之间的全部关系。
webSocket 连接到您传递给
webSocketServer()
构造函数的特定路径,因为您似乎已经知道该路径。一旦它们连接起来,它们就会保持连接并形成一个 TCP 管道,您可以将数据包从客户端发送到服务器或从服务器发送到客户端。没有为此使用 Express 路由。如果需要,您可以通过创建消息名称作为 webSocket 有效负载的一部分,在 webSocket 消息中创建自己的消息处理(这是 webSockets 之上的 socket.io 层为您做的事情),但它与此无关那时与 Express 。这就是您选择处理传入的 webSocket 数据包的方式。
您的具体问题是什么?也许如果您说出需要帮助的具体问题,我们可以提供进一步的帮助。
要处理传入的 webSocket 消息,您可以按照 ws 服务器文档 中的示例进行操作:
为了进一步分解它以处理不同类型的传入 webSocket 消息,您必须创建自己的消息格式,您可以在该格式上进行分支,或者在客户端和服务器上使用 socket.io 来为您执行此操作。
You do not use Express routers with webSockets. That's not the proper architecture for webSockets. Your webSocket server can share an http server with Express, but that's pretty much all the two have to do with one another.
webSockets connect on a particular path which you pass to the
webSocketServer()
constructor as it appears you already know. Once they are connected they stay connected and form a TCP pipe that you can send packets of data from client to server or from server to client. There is no Express routing used for that.You can create your own message handling within a webSocket message by creating a message name as part of the webSocket payload if you want (this is something that the socket.io layer on top of webSockets does for you), but it has nothing to do with Express at that point. That's just in how you choose to handle the incoming webSocket packets.
What is your specific problem? Perhaps if you stated the specific problem you want help with, we could provide further assistance.
To handle incoming webSocket messages, you can follow the example in the ws server doc:
To further break up this to handle different types of incoming webSocket messages, you have to create your own message format that you can branch on or use socket.io instead on both client and server that does that for you.