Skip to main content

websocket client or server, easy to use.

Project description

SocketMan

  • websockets 包的易用封装。
  • 在独立线程中封装了协程
  • 可以非阻塞方式交互
  • 符合面向过程习惯的send、recv方法

应用场景示例

  • 针对flask+werkzeug的单线程模式服务器无法使用服务websocket的问题,创建独立的websocket服务。

使用方法

启动线程(服务端)

serv = WebsocketServer('0.0.0.0', 5001)
serv.start()

客户端访问地址,服务未运行时为None

print(serv.url)

设置处理接收消息的回调函数

# 有消息传入时会随消息附带wsid, wsid为传入消息的连接编号
@serv.ON_RECV
def func( txt, wsid):
    print(txt)

用recv方法接收,默认阻塞(不推荐)

serv.recv()

# 当设置wsid!=None时只接收对应wsid连接发送的数据
# block=False为非阻塞模式,无消息返回None
# 接收过程中连接关闭则raise一个RuntimeError
serv.recv(wsid=None, block=True)

发送消息到客户端

# 单个连接可以不带wsid参数,即wsid默认为None时将对所有连接广播消息
serv.send(txt)
serv.send(txt, wsid=None)

关闭线程

# 注意,线程启动关闭是一次性行为,如需再次使用需要重新实例化
serv.stop()

检查是否已在运行

serv.is_alive()

启动线程(客户端)

客户端与服务端的调用逻辑基本一致

client = WebsocketClient('ws://localhost:5001')
client.start()

设置处理接收消息的回调函数

@client.ON_RECV
def func( txt ):
    print(txt)

用recv方法接收,默认阻塞(不推荐)

client.recv()

发送消息到服务端

client.send(txt)

关闭线程

# 注意,线程启动关闭是一次性行为,如需再次使用需要重新实例化
client.stop()

检查是否已在运行

client.is_alive()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

socketman-0.1.4.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

socketman-0.1.4-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file socketman-0.1.4.tar.gz.

File metadata

  • Download URL: socketman-0.1.4.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for socketman-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1f1f2c7837cc22aa896ee03d3e0129b6ec1eca4a80669726711ae0415c73a8fb
MD5 bc12296862caad000e19d24939f4708f
BLAKE2b-256 d1c039c082f8a229bff85549ce986d8c122b25578baad6d4d390d8956a3db9e2

See more details on using hashes here.

File details

Details for the file socketman-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: socketman-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for socketman-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c0a69a9b90ff49b3626316e5d027f5b91a9bd783cfaa0bc9d1ab676c1bec9c2a
MD5 f6fd52fe73f6384b97099e817b2090eb
BLAKE2b-256 1c9825748e8d48eba812aebbf0bcb0e3bf7f867e8dbe19b16dd672c3adb11a93

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page