本文介绍了socket.io 中的“socket"对象实际上是什么?如何确保其使用最佳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个项目,我必须在其中使用 nodejssocket.io 开发基于 Web 的棋盘游戏.问题是,我直接从 Android 开发跳到了 Web 开发,所以我在理解概念方面遇到了一些问题.

I currently got a project in which I have to develop a web based board game using nodejs and socket.io. The thing is, I have jumped directly from Android development to Web dev and so I am facing some issues understanding concepts.

我已经实现了一些代码,其中客户端连接到服务器 (app.js)、发送消息、接收消息等.我还添加了一个小聊天窗口供玩家聊天.它的工作.我对发送消息和加入房间内容有了粗略的想法.

I have already implemented some code where the client connects to the server (app.js), sends message, receives message etc. I also added a small chat window for players to chat. Its working. I got rough idea about emitting messages and joining room stuffs.

问题是,我不确定如何充分利用用户连接时服务器收到的 socket 对象.我应该保存那个socket对象以备将来使用吗?或者我只是将用户添加到房间,因此可以向单个用户发送消息并且不关心将套接字保存到变量?在这个项目的未来开发中,我需要再次使用 socket 对象来做某些事情的机会有多大?我确实在控制台中输出了套接字对象以查看它包含哪些信息,但是,这对我来说太难理解了.

The thing is, I am not sure how to make good use of the socket object that server receives when a user connects. Should I save that socket object somewhere for future use? Or I just add users to rooms so emitting message to a single user is possible and do not care about saving the socket to a variable? What are the chances that I will need to use the socket object again in the future development of this project for doing certain things? I did output the socket object in console to see what information it contained but well, that was too much for me to understand.

推荐答案

在 socket.io 中,socket 对象是一个 Javascript 对象,socket.io 使用它来跟踪给定套接字的状态.它有一些方法和属性,可用于某些目的,为了使用它们,您必须获得对所需套接字对象的引用.

In socket.io, the socket object is a Javascript object which socket.io uses to keep track of the state of a given socket. It has methods and properties on it that are useful for you to use for some purposes and in order to use those, you would have to get a reference to the desired socket object.

但是,您不一定需要自己保存套接字对象,因为 socket.io 服务器基础结构保存了所有这些对象,并将它们保存在一个有组织的结构中,如果您想或需要.

But, you do not necessarily need to save the socket object yourself because the socket.io server infrastructure saves all of them and keeps them in an organized structure where you can find them at any time in the future if you want to or need to.

在闭包中保留套接字引用也是很常见的,它允许您接收消息并响应发送该消息的套接字,就像在这种类型的方案中一样:

It is also common to keep socket references in a closure that allow you to receive messages and respond to the socket that send that message as in this type of scheme:

io.on('connection', function(socket){
    socket.on('getDataX', function(requestArgs) {
        // process the getDataX request

        // send response to that same socket
        socket.emit('replyDataX', ....);
    });
});

或者,您可以使用以下命令向所有套接字广播:

Or, you can broadcast to all sockets with:

io.emit('update', ...);

您可以通过多种方式访问​​ socket.io 存储的已连接套接字列表.例如:

You can get access to the list of connected sockets stored by socket.io in a number of ways. For example:

io.sockets.connected

是一个对象,其中的属性是套接字 id 和套接字对象的值,因此可以在给定 id 时找到套接字对象,或者可以迭代所有连接的套接字.

is an object where the properties are socket ids and the values of the socket objects so one can find a socket object when given an id or can iterate all the connected sockets.

您也可以将自己的属性添加到套接字对象中.一些聊天系统会在 socket 对象中添加用户名,这样你也可以通过他们的用户名识别或找到给定的用途.

You can also add your own properties to the socket object. Some chat systems will add a username to the socket object so you can also identify or find a given use by their username.

这篇关于socket.io 中的“socket"对象实际上是什么?如何确保其使用最佳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:23