socket.io客户端库
socketIO-client-nexus的Python项目详细描述
Socketio客户端
这是一个用于python的客户机库。您可以使用它为socket.io服务器编写测试代码。
这是实现socket.io 2.x更改的分叉版本。您可以在这里找到原始的
请注意,此版本实现了不向后兼容的socket.io protocol 1.x。如果要使用socket.io协议0.9(与gevent socketio兼容)进行通信,请使用socketio client 0.5.7.2
安装
在隔离的环境中安装软件包。
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client
用法
激活隔离环境。
VIRTUAL_ENV=$HOME/.virtualenv source $VIRTUAL_ENV/bin/activate
启动socket.io服务器。
cd $(python -c "import os, socketIO_client;\ print(os.path.dirname(socketIO_client.__file__))") DEBUG=* node tests/serve.js # Start socket.io server in terminal one DEBUG=* node tests/proxy.js # Start proxy server in terminal two nosetests # Run tests in terminal three
有关调试信息,请先运行这些命令。
import logging logging.getLogger('socketIO-client').setLevel(logging.DEBUG) logging.basicConfig()
发射,
from socketIO_client import SocketIO, LoggingNamespace with SocketIO('localhost', 8000, LoggingNamespace) as socketIO: socketIO.emit('aaa') socketIO.wait(seconds=1)
通过回调发出。
from socketIO_client import SocketIO, LoggingNamespace def on_bbb_response(*args): print('on_bbb_response', args) with SocketIO('localhost', 8000, LoggingNamespace) as socketIO: socketIO.emit('bbb', {'xxx': 'yyy'}, on_bbb_response) socketIO.wait_for_callbacks(seconds=1)
定义事件。
from socketIO_client import SocketIO, LoggingNamespace def on_connect(): print('connect') def on_disconnect(): print('disconnect') def on_reconnect(): print('reconnect') def on_aaa_response(*args): print('on_aaa_response', args) socketIO = SocketIO('localhost', 8000, LoggingNamespace) socketIO.on('connect', on_connect) socketIO.on('disconnect', on_disconnect) socketIO.on('reconnect', on_reconnect) # Listen socketIO.on('aaa_response', on_aaa_response) socketIO.emit('aaa') socketIO.emit('aaa') socketIO.wait(seconds=1) # Stop listening socketIO.off('aaa_response') socketIO.emit('aaa') socketIO.wait(seconds=1) # Listen only once socketIO.once('aaa_response', on_aaa_response) socketIO.emit('aaa') # Activate aaa_response socketIO.emit('aaa') # Ignore socketIO.wait(seconds=1)
在命名空间中定义事件。
from socketIO_client import SocketIO, BaseNamespace class Namespace(BaseNamespace): def on_aaa_response(self, *args): print('on_aaa_response', args) self.emit('bbb') socketIO = SocketIO('localhost', 8000, Namespace) socketIO.emit('aaa') socketIO.wait(seconds=1)
定义标准事件。
from socketIO_client import SocketIO, BaseNamespace class Namespace(BaseNamespace): def on_connect(self): print('[Connected]') def on_reconnect(self): print('[Reconnected]') def on_disconnect(self): print('[Disconnected]') socketIO = SocketIO('localhost', 8000, Namespace) socketIO.wait(seconds=1)
在单个套接字上定义不同的命名空间。
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client0
通过SSL连接(https://github.com/invisibleroads/socketio-client/issues/54" rel="nofollow">https://github.com/invisibleroads/socketio-client/issues/54)。
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client1
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client2
永远等待。
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client3
不要永远等待。
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client4
许可证
此软件在麻省理工学院许可证下提供。
学分
- guillermo rauch编写了socket.io规范。
- hiroki ohtani 撰写了 websocket客户端
- roderick hodgson 为python客户机到socket.io服务器的原型编写了一个
- alexandre bourget 编写了 gevent socketio ,这是用python编写的socket.io服务器。
- paul kienzle , zac lee , josh vanderlinden , ian fitzpatrick ,卢卡斯•克莱因, 瑞•奇科里亚 , 特拉维斯•奥多姆 , 帕特里克•休伯 ,布拉德·坎贝尔s://github.com/dabidan" rel="nofollow">daniel, sean arietta , sacha stafyniak 提交了扩展socket.io协议支持的代码。
- bernard pratz , francis bull 编写了原型以支持xhr轮询和jsonp轮询。
- Joe Palmer 赞助开发。
- eric chen , denis zinevich , thiago hersan , nayef copty , jórgen karlsson , branden ghena , tim landscheidt , matt porritt , matt dainty , thomaz de oliveira dos reis , felix kónig , george wilson , andreas strikos , alesio sergi claudio yacarini , khairi hafsham , Robbie Clarken 建议如何使连接更加可靠。
- merlijn van deen , frederic sureau , marcus cobden ,德鲁·哈奇森, 乌尔德 , 亚当·凯瑟 , 亚历克斯·蒙克 , vishal p r , john vandenberg , thomas grainger , daniel quinn , adric worley , adam roses wight , jan v_el_k 提议的变更,使图书馆对您更加友好和实用!