2011-07-14 37 views
19

Sto cercando di ottenere un client Python che comunica con un server Node.js utilizzando Socket.io 0.7, inviando un evento personalizzato al server.Formattazione dei messaggi da inviare al server socket.io node.js dal client python

Sulla base del riferimento Socket.io che ho trovato su GitHub e il seguente WebSocket Python library.

Ecco il mio codice finora:

server del nodo

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

Python cliente

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() 

output della console Nodo

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 

console Python uscita

Done 
1:: 
6:::1 
Closing connection 

ora sembra l'evento presa non viene attivato, nonostante il server risponde con ACK. Quindi il messaggio viene correttamente ricevuto, ma suppongo, non formattato correttamente per socket.io per attivare un evento.

Non pensavo inquadratura fosse necessario, tuttavia Archie1986 sembra essere d'accordo sulla sua risposta a questo: Socket.IO Client Library in Python

Cosa potrei fare male qui?

+1

risolto. Accetterò la mia risposta non appena ho il permesso di farlo. Il problema era nell'uso di singole virgolette piuttosto che doppie per JSON. – rod

risposta

14

risolto. Avevo bisogno di usare virgolette. Le virgolette singole non sono JSON valide. Woops.

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

diventa:

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

ho avvolto rod s' di ricerca in un 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) 
+0

Cosa fa socketIO.wait? è secondi = 1 un timeout? – lmc

Problemi correlati