我从socket.io + node.js开始,我知道如何在本地发送消息和广播socket.broadcast.emit()功能: - 所有连接的客户端都收到相同的消息.
现在,我想知道如何向特定客户端发送私人消息,我的意思是一个套接字用于2人之间的私人聊天(客户端到客户端流).谢谢.
我看到有关于此主题的问题,但具体代码未概述.假设我只想向第一个客户端发射.
例如(在events.py中):
clients = []
@socketio.on('joined', namespace='/chat')
def joined(message):
"""Sent by clients when they enter a room.
A status message is broadcast to all people in the room."""
#Add client to client list
clients.append([session.get('name'), request.namespace])
room = session.get('room')
join_room(room)
emit('status', {'msg': session.get('name') + ' has entered the room.'}, room=room)
#I want to do something like this, emit message to the first client
clients[0].emit('status', {'msg': session.get('name') + ' has entered the room.'}, room=room)
Run Code Online (Sandbox Code Playgroud)
这怎么做得好?
谢谢