简体   繁体   English

Socket.IO,多个用户的不同功能

[英]Socket.IO, multiple users different functionality

I'm trying to create an multi-user and multi-permission system that uses socket.io in order to make a lot of the features real-time and fluid. 我正在尝试创建一个使用socket.io的多用户和多权限系统,以使许多功能实时且流畅。

I have everything setup and functionally working on page load, querying databases and pulling through relevant data based on the user, their permissions and what groups they're a part of however how would I go about assigning this user a socket, that retains this 'user info' such as their used id along with other things you'd commonly find within session data? 我已经进行了所有设置,并在页面加载,查询数据库以及根据用户,他们的权限以及它们属于哪个组的基础上获取相关数据的功能上进行了工作,但是我将如何为该用户分配一个套接字,并保留此“用户信息”(例如其使用的ID)以及您通常在会话数据中找到的其他内容?

From what I can see, the socket ID resets and changes whenever a page is loaded, refreshed or closed so I can't store this ID in the database temporarily against the user to query against - I'm a little stuck on the best way / practice to go about this. 据我所知,每当页面被加载,刷新或关闭时,套接字ID都会重置和更改,因此我无法将该ID临时存储在数据库中,以供用户查询-我在最佳方法上有些困惑/练习一下。

Edit: I should mention that my application is already built and fully functional aside from the socket side of things using Express. 编辑:我应该提一下,我的应用程序已经被构建,并且使用Express可以实现套接字功能之外的全部功能。

Your are correct that every new page the user navigates to will close the prior socket.io connection and create a new one and each new socket.io connection will have a new socket.id . 您是正确的,用户浏览到的每个新页面都将关闭先前的socket.io连接并创建一个新页面,并且每个新的socket.io连接都将具有一个新的socket.id The socket.id is not meant to be used as a persistent reference to a specific user. socket.id并不是要用socket.id特定用户的持久引用。 It is only a reference to a specific connection. 它仅是对特定连接的引用。

Instead, what it sounds like you want is some sort of user session that you can persistently identify the user every time there's a new socket.io connection. 相反,听起来像您想要的是某种用户会话,您可以在每次建立新的socket.io连接时持久地标识该用户。 You would then use your own userID as a persistent id that can apply to each incoming socket.io connection. 然后,您将使用自己的userID作为持久ID,该ID可以应用于每个传入的socket.io连接。

The usual scheme here is to set up a regular http session using something like express-session and/or passport. 这里通常的方案是使用快速会话和/或护照之类的方式建立常规的http会话。 Once you have that, you have a means of identifying all incoming http connections as to what user they belong to. 一旦有了这些,就可以识别所有传入的HTTP连接以及它们属于哪个用户。 Since every socket.io connection starts as an http request, that means you can identify all socket.io connections to. 由于每个socket.io连接都是从http请求开始的,所以这意味着您可以标识所有的socket.io连接。 To do that, you can either use both express-session and express-socket.io-session to do most of the work for you or you can set things up yourself as described here: How to share sessions with Socket.IO 1.x and Express 4.x? 为此,您可以同时使用express-session和express-socket.io-session来完成大部分工作,也可以按照以下说明自行设置: 如何与Socket.IO 1.x共享会话和Express 4.x?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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