我在此处Chat server example设置并启动了聊天服务器示例。 我可以使用broswer和java socket.io客户端输入登录名并连接并发送消息。
但我无法使用python socketio-client登录聊天服务器。 下面是python客户端代码: $ cat client.py,端口更改为3001,原始为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)
以下是运行时的日志:
$ 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
在节点聊天服务器上,我添加了DEBUG = *来查看日志,当python客户端测试程序运行时,服务器输出如下:
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
作为比较,当另一个socket.io客户端(例如:来自Web浏览器,http://localhost:3001)登录服务器输出时:
$ 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
问题: 有没有任何示例使用python socket.io客户端与聊天示例服务器? 或者我的测试代码有什么问题?或者有人可以提示调试吗?