简体   繁体   中英

Socket.io and request and response objects

I'm quite new on node.js and now I'm learning socket.io. I'm developing an app step by step so, at this time, I have an app that can login an user, do crud operation in mysql and mongodb and upload files, all these operations are manage with some web pages with HTML and javascript technologies launched directly from restify. After that I'm tring to add socket functionality to, at this time, simple print who is online. So, before I have something like:

server.get('/login', function(req, res, next){ ... });

and now I have something like:

socket.on("login", function (req, res, next){ ... });

but, naturally, req and res are undefined! Are there the same objects into socket.io?

To my understanding, you want to pass values back and forth in your request and response using socket.io.

Yes it is possible to do that and you syntax should be something like this...

Using express.js:

io.on('login', function(req){
client.emit('response event', { some: 'data' });

Note: when using emit you send the data to everyone, you have other methods like .broadcast(), .to(), etc.. for other use cases refer to socket.io github for better understanding And lastly, inside emit you define the function you want to call on the client side and the data you want to send to the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM