使用用户身份验证,发送/接收数据创建Node.js + socket.io服务器/客户端

时间:2022-07-24 23:53:36

I need to create web-based system with users live interaction (sending/receiving data) based on Node.js + socket.io. I've googled a lot of materials about Node.js and websockets but there are very much different solutions (using different frameworks and technologies) according different parts of my needs.

我需要创建基于Web的系统,用户可以基于Node.js + socket.io进行实时交互(发送/接收数据)。我搜索了很多关于Node.js和websockets的资料,但根据我的需求,我们有不同的解决方案(使用不同的框架和技术)。

Please recommend some good and effective solution to create such web-based system where users could authenticate themselves using PHP sessions and some users directly using node.js require('mysql')..

请推荐一些好的有效解决方案来创建这样的基于Web的系统,用户可以使用PHP会话进行身份验证,一些用户直接使用node.js require('mysql')。

How to keep live sessions with Node.js+socket.io after user authentication and keep updated live results even if browser window was unexpectedly closed?

如何在用户身份验证后保持与Node.js + socket.io的实时会话,并保持更新的实时结果,即使浏览器窗口意外关闭?

How to send/receive data arrays or only changed data keys values from one user to another and send data only to specified group of users from administrator?

如何从一个用户向另一个用户发送/接收数据阵列或仅更改数据键值,并仅向管理员发送指定的用户组?

Please provide me some information and/or links according your experience with similar web-based system needs.

请根据您的经验提供一些信息和/或链接,以及类似的基于Web的系统需求。

Thanks in advance for your help and patience.

提前感谢您的帮助和耐心。

1 个解决方案

#1


0  

You could actually build what (I think) you're describing purely using Node.JS, Socket.IO, session sockets, Redis, and passport.js

您实际上可以构建(我认为)纯粹使用Node.JS,Socket.IO,会话套接字,Redis和passport.js的内容。

http://passportjs.org/ is an excellent library for doing user login in Node.JS

http://passportjs.org/是一个用于在Node.JS中进行用户登录的优秀库

http://redis.io/topics/quickstart can be used as a session store, along with session sockets. By using a session store, you ensure that as long as the session store is active, a user's session will not be destroyed by simply closing the browser window. You would have to either kill the session store, destroy the session, or configure the session store to automatically destroy the session after some time

http://redis.io/topics/quickstart可以用作会话存储,以及会话套接字。通过使用会话存储,您可以确保只要会话存储处于活动状态,只需关闭浏览器窗口就不会破坏用户的会话。您必须终止会话存储,销毁会话或配置会话存储以在一段时间后自动销毁会话

To send messages over a socket.io socket to a particular user, you can save his socket when he first connects. It would look something like this

要通过socket.io套接字向特定用户发送消息,您可以在首次连接时保存其套接字。它看起来像这样

var storedSockets = {}
io.socket.on("connection", function (socket) {
    storedSockets[socket.id] = socket
})

Now, whenever you want to connect to that particular socket, you can access it through storedSockets like this

现在,只要您想连接到该特定套接字,就可以通过这样的storedSockets访问它

storedSockets[socket_id_of_required_socket].emit("testing", {})

Of course, you need to store the socket id somewhere safe and have a reliable way to retrieve the one you want. For example, if you have a Users table in your database, you could store each users socket id with his record. For groups, just find all the users that fall in a group, and one after the other retrieve their sockets and send the messages

当然,您需要将套接字ID存储在安全的地方,并且有可靠的方法来检索您想要的那个。例如,如果数据库中有Users表,则可以使用其记录存储每个用户的套接字ID。对于组,只需查找属于组的所有用户,然后逐个检索其套接字并发送消息

#1


0  

You could actually build what (I think) you're describing purely using Node.JS, Socket.IO, session sockets, Redis, and passport.js

您实际上可以构建(我认为)纯粹使用Node.JS,Socket.IO,会话套接字,Redis和passport.js的内容。

http://passportjs.org/ is an excellent library for doing user login in Node.JS

http://passportjs.org/是一个用于在Node.JS中进行用户登录的优秀库

http://redis.io/topics/quickstart can be used as a session store, along with session sockets. By using a session store, you ensure that as long as the session store is active, a user's session will not be destroyed by simply closing the browser window. You would have to either kill the session store, destroy the session, or configure the session store to automatically destroy the session after some time

http://redis.io/topics/quickstart可以用作会话存储,以及会话套接字。通过使用会话存储,您可以确保只要会话存储处于活动状态,只需关闭浏览器窗口就不会破坏用户的会话。您必须终止会话存储,销毁会话或配置会话存储以在一段时间后自动销毁会话

To send messages over a socket.io socket to a particular user, you can save his socket when he first connects. It would look something like this

要通过socket.io套接字向特定用户发送消息,您可以在首次连接时保存其套接字。它看起来像这样

var storedSockets = {}
io.socket.on("connection", function (socket) {
    storedSockets[socket.id] = socket
})

Now, whenever you want to connect to that particular socket, you can access it through storedSockets like this

现在,只要您想连接到该特定套接字,就可以通过这样的storedSockets访问它

storedSockets[socket_id_of_required_socket].emit("testing", {})

Of course, you need to store the socket id somewhere safe and have a reliable way to retrieve the one you want. For example, if you have a Users table in your database, you could store each users socket id with his record. For groups, just find all the users that fall in a group, and one after the other retrieve their sockets and send the messages

当然,您需要将套接字ID存储在安全的地方,并且有可靠的方法来检索您想要的那个。例如,如果数据库中有Users表,则可以使用其记录存储每个用户的套接字ID。对于组,只需查找属于组的所有用户,然后逐个检索其套接字并发送消息