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.

user784756
  • 2,363
  • 4
  • 28
  • 44

1 Answers1

1

Once you have your session id you can create new method in custom mxin class which should look something like this:

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

than call it whenever you need it.

Cheers

Velin Georgiev
  • 2,359
  • 1
  • 15
  • 21