invisibleroads / socketIO-client

A socket.io client library for Python
http://pypi.python.org/pypi/socketIO-client
MIT License
447 stars 205 forks source link

Python SocketIO client can't connect to Socket.IO chat example server #173

Open tonyho opened 6 years ago

tonyho commented 6 years ago

I setup and launch the chat server example at here Chat server example. And I can enter login and connect and send message using the broswer and the java socket.io client.

But I can't login to the chat server using python socketio-client. Below is the python client code: $ cat client.py, the port is change to 3001, original is 3000

#!/usr/bin/env python

from socketIO_client import SocketIO, BaseNamespace

class MyNamespace(BaseNamespace):

    def connect(self, *args):
        print('on_connect', args)
    def on_connect(self, *args):
        print('on_connect', args)

    def user(self, *args):
        print('on_aaa_response', args)

    def on_aaa_response(self, *args):
        print('on_aaa_response', args)

socketIO = SocketIO('localhost', 3001)
namespace = "chat"
event_name = "connect"
my_namespace = socketIO.define(MyNamespace, "/")
#my_namespace = socketIO.define(MyNamespace, "/my_namespace")

my_namespace.emit(event_name, {"message": "from python socketio-client"})
socketIO.wait(seconds=1)

Below is the log when run it:

 $ python3 client.py
Traceback (most recent call last):
  File "client.py", line 18, in <module>
    socketIO = SocketIO('localhost', 3001)
  File "/home/hexiongjun/.local/lib/python3.5/site-packages/socketIO_client/__init__.py", line 353, in __init__
    resource, hurry_interval_in_seconds, **kw)
  File "/home/hexiongjun/.local/lib/python3.5/site-packages/socketIO_client/__init__.py", line 54, in __init__
    self._transport
  File "/home/hexiongjun/.local/lib/python3.5/site-packages/socketIO_client/__init__.py", line 62, in _transport
    self._engineIO_session = self._get_engineIO_session()
  File "/home/hexiongjun/.local/lib/python3.5/site-packages/socketIO_client/__init__.py", line 76, in _get_engineIO_session
    transport.recv_packet())
StopIteration

And at the node chat server, I add the DEBUG=* to view the logs, when the python client test program run, the server outputs following:

engine intercepting request for path "/socket.io/" +6s
  engine handling "GET" http request "/socket.io/?EIO=3&t=1514282079724-0&transport=polling" +0ms
  engine handshaking client "g-cU6KbyQ70kQgVRAAAL" +0ms
  engine:socket sending packet "open" ({"sid":"g-cU6KbyQ70kQgVRAAAL","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +0ms
  engine:socket sending packet "message" (0) +0ms
  engine:polling setting request +0ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "97:0{"sid":"g-cU6KbyQ70kQgVRAAAL","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40" +1ms
  engine:socket executing batch send callback +0ms
  socket.io:server incoming connection with id g-cU6KbyQ70kQgVRAAAL +0ms
  socket.io:client connecting to namespace / +0ms
  socket.io:namespace adding socket to nsp / +0ms
  socket.io:socket socket connected - writing packet +0ms
  socket.io:socket joining room g-cU6KbyQ70kQgVRAAAL +0ms
  socket.io:socket packet already sent in initial handshake +0ms
  socket.io:socket joined room g-cU6KbyQ70kQgVRAAAL +1ms

As comparison, when the other socket.io client(eg: from web browser, http://localhost:3001) login the server output followings:

$   socket.io:client client close with reason transport close +11s
  socket.io:socket closing socket - reason transport close +0ms
  socket.io-parser encoding packet {"type":2,"data":["user left",{"username":"Tester","numUsers":1}],"nsp":"/"} +0ms
  socket.io-parser encoded {"type":2,"data":["user left",{"username":"Tester","numUsers":1}],"nsp":"/"} as 2["user left",{"username":"Tester","numUsers":1}] +0ms
  socket.io:client writing packet ["2[\"user left\",{\"username\":\"Tester\",\"numUsers\":1}]"] +1ms
  engine:socket sending packet "message" (2["user left",{"username":"Tester","numUsers":1}]) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "42["user left",{"username":"Tester","numUsers":1}]" +0ms
  express:router dispatching GET / +57m
  express:router query  : / +1ms
  express:router expressInit  : / +0ms
  express:router serveStatic  : / +0ms
  send stat "/home/hexiongjun/Projects/Telink_AutoTest/socket.io/examples/chat/public/index.html" +57m
  send pipe "/home/hexiongjun/Projects/Telink_AutoTest/socket.io/examples/chat/public/index.html" +0ms
  send modified Mon, 04 Dec 2017 09:47:57 GMT +0ms
  send etag W/"2e1-16020eccc10" +1ms
  send content-type text/html +0ms
  send not modified +0ms
  express:router dispatching GET /style.css +40ms
  express:router query  : /style.css +0ms
  express:router expressInit  : /style.css +0ms
  express:router serveStatic  : /style.css +0ms
  send stat "/home/hexiongjun/Projects/Telink_AutoTest/socket.io/examples/chat/public/style.css" +39ms
  send pipe "/home/hexiongjun/Projects/Telink_AutoTest/socket.io/examples/chat/public/style.css" +0ms
  send modified Mon, 04 Dec 2017 09:47:57 GMT +1ms
  send etag W/"74e-16020eccc10" +0ms
  send content-type text/css +0ms
  send not modified +0ms
  socket.io:server serve client 304 +63ms
  express:router dispatching GET /main.js +3ms
  express:router query  : /main.js +1ms
  express:router expressInit  : /main.js +0ms
  express:router serveStatic  : /main.js +0ms
  send stat "/home/hexiongjun/Projects/Telink_AutoTest/socket.io/examples/chat/public/main.js" +4ms
  send pipe "/home/hexiongjun/Projects/Telink_AutoTest/socket.io/examples/chat/public/main.js" +0ms
  send modified Mon, 04 Dec 2017 09:47:57 GMT +0ms
  send etag W/"1db7-16020eccc10" +0ms
  send content-type application/javascript +0ms
  send not modified +1ms
  engine intercepting request for path "/socket.io/" +82ms
  engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=M2IGDKX" +1ms
  engine handshaking client "X-RQOESUEZf2oB9lAAAM" +0ms
  engine:socket sending packet "open" ({"sid":"X-RQOESUEZf2oB9lAAAM","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +0ms
  engine:socket sending packet "message" (0) +0ms
  engine:polling setting request +0ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "97:0{"sid":"X-RQOESUEZf2oB9lAAAM","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40" +1ms
  engine:socket executing batch send callback +0ms
  socket.io:server incoming connection with id X-RQOESUEZf2oB9lAAAM +0ms
  socket.io:client connecting to namespace / +0ms
  socket.io:namespace adding socket to nsp / +0ms
  socket.io:socket socket connected - writing packet +0ms
  socket.io:socket joining room X-RQOESUEZf2oB9lAAAM +0ms
  socket.io:socket packet already sent in initial handshake +1ms
  socket.io:socket joined room X-RQOESUEZf2oB9lAAAM +0ms
  engine upgrading existing transport +15ms
  engine:socket might upgrade socket transport from "polling" to "websocket" +0ms
  engine intercepting request for path "/socket.io/" +0ms
  engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=M2IGDKw&sid=X-RQOESUEZf2oB9lAAAM" +0ms
  engine setting new request for existing client +1ms
  engine:polling setting request +0ms
  engine:ws received "2probe" +1ms
  engine:ws writing "3probe" +0ms
  engine:socket writing a noop packet to polling for fast upgrade +101ms
  engine:polling writing "1:6" +0ms
  engine:ws received "5" +4ms
  engine:socket got upgrade packet - upgrading +0ms
  engine:polling closing +0ms
  engine:polling transport discarded - closing right away +1ms
  engine:ws received "42["add user","dd"]" +2s
  engine:socket packet +0ms
  socket.io-parser decoded 2["add user","dd"] as {"type":2,"nsp":"/","data":["add user","dd"]} +0ms
  socket.io:socket got packet {"type":2,"nsp":"/","data":["add user","dd"]} +1ms
  socket.io:socket emitting event ["add user","dd"] +0ms
  socket.io:socket dispatching an event ["add user","dd"] +0ms
  socket.io:client writing packet {"type":2,"data":["login",{"numUsers":2}],"nsp":"/"} +0ms
  socket.io-parser encoding packet {"type":2,"data":["login",{"numUsers":2}],"nsp":"/"} +0ms
  socket.io-parser encoded {"type":2,"data":["login",{"numUsers":2}],"nsp":"/"} as 2["login",{"numUsers":2}] +0ms
  engine:socket sending packet "message" (2["login",{"numUsers":2}]) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "42["login",{"numUsers":2}]" +1ms
  socket.io-parser encoding packet {"type":2,"data":["user joined",{"username":"dd","numUsers":2}],"nsp":"/"} +0ms
  socket.io-parser encoded {"type":2,"data":["user joined",{"username":"dd","numUsers":2}],"nsp":"/"} as 2["user joined",{"username":"dd","numUsers":2}] +0ms
  socket.io:client writing packet ["2[\"user joined\",{\"username\":\"dd\",\"numUsers\":2}]"] +0ms
  engine:socket sending packet "message" (2["user joined",{"username":"dd","numUsers":2}]) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "42["user joined",{"username":"dd","numUsers":2}]" +0ms

Is there any example to use the python socket.io client with chat example server? Or any wrong with my test code? Or Can someone give a hint to debug?

duvallj commented 6 years ago

Haven't looked to hard at this, but seems possibly related to https://github.com/feus4177/socketIO-client-2/issues/10. Basically this library (and the fork in the link) doesn't support socket.io protocol version 2.0. I remeber seeing a fork that did, but forget which one it is.

claradaia commented 6 years ago

I had a similar issue, but because I had forgotten to update the protocol version on the server. In case someone else ends up here looking for it, the fork with support to 2.0 is socketIO-client-nexus.

lshe7842 commented 6 years ago

Verified that with socketIO-client-nexus, the issue is fixed. Note that you will need to change import from socketIO_client to socketIO_client_nexus.

ThankYou111 commented 4 years ago

Thank you guys soooo much. I have just created an account to show my appreciation. I was stuck on this for ages and couldn't see what was wrong with my code.