向 Socket.IO 1.0 中的特定 ID 发送消息 [英] Sending message to a specific ID in Socket.IO 1.0

查看:25
本文介绍了向 Socket.IO 1.0 中的特定 ID 发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据发送到一个特定的套接字 ID.

I want to sent data to one specific socket ID.

我们曾经能够在旧版本中做到这一点:

We used to be able to do this in the older versions:

io.sockets.socket(socketid).emit('message', 'for your eyes only');

我将如何在 Socket.IO 1.0 中做类似的事情?

How would I go about doing something similar in Socket.IO 1.0?

推荐答案

在 socket.io 1.0 中,他们为此提供了更好的方法.每个套接字通过 self id 自动加入一个默认房间.检查文件:http://socket.io/docs/rooms-and-namespaces/#default-room

In socket.io 1.0 they provide a better way for this. Each socket automatically joins a default room by self id. Check documents: http://socket.io/docs/rooms-and-namespaces/#default-room

因此您可以使用以下代码通过 id 发送到套接字:

So you can emit to a socket by id with following code:

io.to(socketid).emit('message', 'for your eyes only');

这篇关于向 Socket.IO 1.0 中的特定 ID 发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆