0

I've been trying to use the python gevent-socketio library to try to send private messages by socket id, but haven't had any luck. I figured out one can send a message to the socket using the namespace via:

pkt = dict(type="event",
name="getBuddies",
args=' '.join(buddies),
endpoint=self.ns_name)
self.socket.send_packet(pkt)

and I can get and store a socket id from self.socket.sessId, but I do not know how to send a message to a specific socket id.

4

1 回答 1

1

获得会话 ID 后,您可以在自定义 mxin 类中创建新方法,该方法应如下所示:

class CustomBroadcastMixin(object):
    ...
    def broadcast_to_socket(self, session_id, event, *args):
        """
        Broadcast to one socket only. 
        Aims to be used for wishper messages.
        """
        returnValue = False
        pkt = dict(type="event",
                   name=event,
                   args=args,
                   endpoint=self.ns_name)

        try:
            for sessid, socket in self.socket.server.sockets.iteritems():
                if unicode(session_id) == unicode(sessid):
                    socket.send_packet(pkt)
                    returnValue = True
                    break
        except:
            app.logger.exception('')

        return returnValue

而不是在需要时调用它。

干杯

于 2013-09-18T13:45:56.307 回答