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.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

socketman-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: socketman-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 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.0.tar.gz
Algorithm Hash digest
SHA256 b543237f46f2b72b2273a853b2954c017a4f012dfb0916311e4fb6cddbde666b
MD5 820ec751cc6d3c36469d7c6f8d35fd18
BLAKE2b-256 7f1a2603dddb9ef329ce36673037bc7365ca80cdfa1d7f72e1eca9e6bce94080

See more details on using hashes here.

File details

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

File metadata

  • Download URL: socketman-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70d2cefd4c32876b04916443f5d2926eaae2dbb163894da1abd3f34e1b61c7ce
MD5 393c0c6fb7498d7ac1d3cc6b642c7a0d
BLAKE2b-256 8c9fd54d88655134431ac0223808d222e6cb39bcd884b0e6772a07d3fe4aacdf

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