English 中文(简体)
socket.io 服务器端加上 Express, 在 Express 端点上创建 socket. on
原标题:socket.io server side with express, create socket.on on express endpoint
var esocket; io.on( connection , (socket) => { esocket = socket //works intermittent }) app.get( /launchtask , (req, res) => { var taskID = CreateID() // or should it have been ? esocket = io.sockets.sockets.get( req.body.socketid ) // doesn t seem to work either. esocket.on( taskID ,(result) => { io.emit( taskID , result) }) }) I wonder if it s the correct way of using socket outside of io.on() ? I want server to listen to worker to long running result and broadcast to web client with an ID. worker.js socket.emit(taskId, blah) frontend.js socket.on(taskId, ...
问题回答
I wonder if it s the correct way of using socket outside of io.on() ? No, it is not correct. This type of code will not work in a server that serves multiple users. You re jamming a socket from a particular client into a module-level variable and then attempting to use that LATER when an incoming request is made that may or may not be from that same client. This type of design will not work properly in a server with more than one possible client. Since your question does not describe the actual problem you re trying to solve (it just shows placeholder code), we can t really advise what a solution would be.




相关问题
intercept a log from winstonjs and inject arguments

Is it possible to inject an argument into a log such as error, debug or info using the winston configuration (createLogger)? I d like to be able to intercept the log and inject an object into each log

Why can t I pass my token without hardcoding?

If I hardcode the token from console.log(token); it works but doesn t work like this const jwt = require( jsonwebtoken ); module.exports = async (req, res, next) => { const authorization = req....

Writing to files in Node.js

I ve been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

热门标签