简体   繁体   English

如果 socketIO 生成的 id 是自动生成的,如何在两个用户之间建立通信?

[英]How to establish a communication between two users, if the id generated by the socketIO are auto generated?

Hello I hope you are very well, I would like to ask you a question that I have not been able to deduce the answer to.你好,我希望你一切都好,我想问你一个我无法推断出答案的问题。

In a course I was seeing that to send a message to a specific user with socketIO.js the to () method is used and the id as a parameter, but I have a doubt, that id is auto generated by socketIO as far as I understand, so I would like to know How can the frontend know that id?在一门课程中,我看到要使用 socketIO.js 向特定用户发送消息,使用了 to () 方法并将 id 作为参数,但我有疑问,据我所知,该 id 是由 socketIO 自动生成的明白了,所以我想知道前端怎么知道那个id? The example that I saw in the course does it from the console and sends it directly to the method with the id that it already knows, then that example is not so real, what I would like to know in itself How is it that it performs a one-to-one chat if the id is autogenerated by the socket?我在课程中看到的例子是从控制台做的,直接发送给它已经知道的id的方法,那么那个例子不是那么真实,我想知道它本身是如何执行的如果 id 是由套接字自动生成的一对一聊天? I don't know if I understand.我不知道我是否理解。

For example, to start a conversation with another user, you can click on a button, trigger an event that makes emit, send the id of the user who writes, the event that should trigger the backend with socket, but my question is how does it taste like who send the message?例如,要开始与另一个用户的对话,您可以单击一个按钮,触发一个发出的事件,发送写入的用户的 id,应该用套接字触发后端的事件,但我的问题是如何它尝起来像谁发送消息? How do you know the id of who is being sent to when establishing communication between 2 users for the first time? 2个用户第一次建立通信时如何知道发送给谁的id? Obviously this must be sent by frontent as a parameter but also how does the frontend give this id of who will it be sent to?显然,这必须由前端作为参数发送,但前端如何给出将发送给谁的 ID? I don't know if you can store a fixed socket id for a user in a DB or Can you use your DB id to use with sockets?我不知道你是否可以在数据库中为用户存储一个固定的套接字 ID 或者你可以使用你的 DB id 与 sockets 一起使用吗? more than everything is what I can not deduce how it would be done?以上的一切都是我无法推断的,它会怎么做?

I do not know if I understood with the question, more than everything is that, I do not know how it obtains or assigns the id for the target from where the message is sent and if this can be fixed and stored in db or is there any method to this.我不知道我是否理解这个问题,最重要的是,我不知道它如何从发送消息的位置获取或分配目标的 id,以及是否可以修复并存储在 db 中或者是否存在任何方法。

I thank you in advance for your response, and any resources that you share with me about it or if you recommend a course with, I would greatly appreciate it.我提前感谢您的回复,以及您与我分享的任何资源,或者如果您推荐一门课程,我将不胜感激。

as an example I have this method例如我有这个方法

io.on('connection', (client) => {    
client.on('privateMessage', (data)=>{
            const person = user.getPersona(client.id) //get this 
            client.broadcast.to(data.para).emit('privateMessage', createMsj( person.name, data.messages));
        
        });
}

But where does the front-end of the person to receive the message to pass it to the method?但是前端接收消息的人从哪里传递给方法呢?

The front-end will not know the socket.io id of any other clients.前端不会知道任何其他客户端的 socket.io id。 This is where your server needs to be involved.这是您的服务器需要参与的地方。

Each of your users presumably has some username that is displayed in the client UI and this is the name that other clients would know them by.您的每个用户大概都有一些显示在客户端 UI 中的用户名,这是其他客户端认识他们的名称。

So, your server needs to keep a mapping between username and socket.io clientID.因此,您的服务器需要保持用户名和 socket.io clientID 之间的映射。 So, a user can send a request to your server to connect to BobS.因此,用户可以向您的服务器发送请求以连接到 BobS。 Your server then needs to be able to look up BobS, find out if that user is currently connected and, if they are, then what is their socket.id value.然后您的服务器需要能够查找 BobS,查明该用户当前是否已连接,如果是,那么他们的 socket.id 值是多少。 That way, your server can facilitate connecting the two users.这样,您的服务器可以方便地连接两个用户。

This mapping would not typically be kept in a permanent store (such as a database) because the socket.id is a transient value and is only good for the duration of that client's socket.io connection.此映射通常不会保存在永久存储(例如数据库)中,因为 socket.id 是一个瞬态值,仅适用于该客户端的 socket.io 连接期间。 As such, it is more typically just kept in some sort of Javascript data structure (such as a Map object).因此,它通常只保存在某种 Javascript 数据结构中(例如Map对象)。

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

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