2016-09-10 6 views
1

이 주제에 대한 질문이 있지만 구체적인 코드는 설명되어 있지 않습니다. 첫 번째 클라이언트에만 내 보내려한다고 가정 해 보겠습니다. 예를 들어 (events.py에서)flaskio가 특정 사용자에게 내 보냅니다.

는 :

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) 

이 제대로 어떻게하는 것인가?

감사

답변

1

내가 첫 번째 클라이언트에 발광 뒤에 논리를 이해 모르겠지만, 어쨌든,이 방법으로 할 수 있습니다 : 당신이 볼 수 있듯이

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(request.sid) 

    room = session.get('room') 
    join_room(room) 

    # emit to the first client that joined the room 
    emit('status', {'msg': session.get('name') + ' has entered the room.'}, room=clients[0]) 

는, 각 클라이언트가있다 그 자체를위한 공간. 그 방의 이름은 해당 클라이언트에서 이벤트를 처리 할 때 request.sid으로 얻을 수있는 Socket.IO 세션 ID입니다. 따라서 모든 고객에 대해이 sid 값을 저장 한 다음 emit 호출에서 방 이름으로 원하는 값을 사용하면됩니다.

+0

안녕하세요, 빠른 답장을 보내 주셔서 감사합니다. 네, 저는 실제로이 작업을 수행하지 않을 것입니다. 특정 클라이언트로 보낼 예제를 만들려고했습니다. 고마워, 미겔 다시, 매우 감사! – shell

관련 문제