I am using NodeJS, socket.io and express to create an application and I am wondering how can I determinate which socket (id/user) is on which router? For example I have 10 users on my website, and I have 3 routes: /home, /about, /contact. How can I know which user is on which router?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Axios OPTIONS instead of POST Request. Express Res
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- fetch: Getting cookies from fetch response
Firstly to know "which user" itself you'll need a system with sessions and cookies to identify a particular user each time they make a different request. Passport.JS makes this really easy.
Secondly to share this same user among Express route and Socket.io handler would require sharing the session information between the two. For Passport.js there's socket.io-passport that does this.
Lastly, when client-side Socket.io initiates a connection request to the server by
io.connect()
it sets the referrer header of the request to current URL, which can be accessed on server-side withsocket.request.headers.referer
With all that in place you'll finally be able to tell which user is on which route: