Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting messages to send to socket.io node.js server from python client

Tags:

I'm trying to get a Python client talking to a Node.js server using Socket.io 0.7, by sending a custom event to the server.

Based on the Socket.io reference I have found on GitHub, and the following WebSocket Python library.

Here's is my code so far:

Node server

io.sockets.on('connection', function (socket) {   socket.on('newimg', function(data) {         console.log(data);   }); }); 

Python client

def handshake(host, port):     u = urlopen("http://%s:%d/socket.io/1/" % (host, port))     if u.getcode() == 200:         response = u.readline()         (sid, hbtimeout, ctimeout, supported) = response.split(":")         supportedlist = supported.split(",")         if "websocket" in supportedlist:             return (sid, hbtimeout, ctimeout)         else:             raise TransportException()     else:         raise InvalidResponseException()   try:     (sid, hbtimeout, ctimeout) = handshake(HOSTNAME, PORT) #handshaking according to socket.io spec. Except Exception as e:     print e     sys.exit(1) ws = websocket.create_connection("ws://%s:%d/socket.io/1/websocket/%s" % (HOSTNAME, PORT, sid)) print ws.recv() ws.send("2::") ws.send("5:1::{'name':'newimg', 'args':'bla'}") print ws.recv() print "Closing connection" ws.close() 

Node console output

debug - client authorized info  - handshake authorized 12738935571241622933 debug - setting request GET /socket.io/1/websocket/12738935571241622933 debug - set heartbeat interval for client 12738935571241622933 debug - client authorized for  debug - websocket writing 1:: debug - websocket received data packet 2:: debug - got heartbeat packet debug - websocket received data packet 5:1::{'name':'newimg', 'args':'bla'} debug - acknowledging packet automatically debug - websocket writing 6:::1 info  - transport end debug - set close timeout for client 12738935571241622933 debug - cleared close timeout for client 12738935571241622933 debug - cleared heartbeat interval for client 12738935571241622933 debug - discarding transport  

Python console output

Done 1:: 6:::1 Closing connection 

Now it seems the socket event is not being triggered, despite the server responding with ACK. So the message is being correctly received but I am assuming, not formatted properly for socket.io to trigger an event.

I didn't think framing was necessary, however Archie1986 seems to disagree on his response to this: Socket.IO Client Library in Python

What might I be doing wrong here?

like image 426
rod Avatar asked Jul 14 '11 12:07

rod


People also ask

How do I send a Socket message?

The send() function initiates transmission of a message from the specified socket to its peer. The send() function sends a message only when the socket is connected (including when the peer of the connectionless socket has been set via connect()). The length of the message to be sent is specified by the len argument.

How do you send a private message in Socket?

You can try the code below:io.to(socket.id). emit("event", data); whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people.

Can I use Socket.IO in Python?

This projects implements Socket.IO clients and servers that can run standalone or integrated with a variety of Python web frameworks.


2 Answers

I wrapped rod's research into a barebones socket.io client library.

pip install -U socketIO-client python     from socketIO_client import SocketIO     with SocketIO('localhost', 8000) as socketIO:         socketIO.emit('aaa')         socketIO.wait(seconds=1) 
like image 54
Roy Hyunjin Han Avatar answered Oct 21 '22 12:10

Roy Hyunjin Han


Resolved. I needed to use double quotes. Single quotes are not valid JSON. Woops.

ws.send("5:1::{'name':'newimg', 'args':'bla'}") 

Becomes:

ws.send('5:1::{"name":"newimg", "args":"bla"}') 
like image 33
rod Avatar answered Oct 21 '22 11:10

rod