Wednesday 28 December 2016

socket.io in router

Hi, I just started node.js and am currently doing some tutorials out there on google to create a socket.io based chat app.My app has /mainpage where clients can send a request to /chat/:room to create a chatroom, and /chat/:room sends html page (with scripts) back to clients. My issues is, I tried to use io.on('connect') in router.get('/chat/:room, function(...)) so that I can make socket join :room. However, every time clients hit /chat/:room, a new connection is created and causing events occur multiple times. I've been googling all day but haven't meet any satisfying solution... please help!router.get('/chat/:room', function(req, res, next) { // res.sendFile here to send 'chat.html' io.on('connect', function(socket) { // let the client join 'room' specified in the path socket.join(req.query.room); }) });

Submitted December 29, 2016 at 02:07AM by nodestarter

No comments:

Post a Comment