使用Python代码中的SignalR服务器

Den*_*Den 12 javascript python integration signalr signalr.client

将Python与SignalR集成有哪些选择?

Python代码是大型第三方产品的一部分,而不是语言偏好.SignalR服务器提供对现有.NET产品的订阅.

我们想在Python中重用.NET SignalR服务器.

Mal*_*ery 8

在python包索引上有一个SignalR客户端可用,名为“ signalr-client”,它支持一些基本的SignalR功能来源

它与Python v2和v3兼容。

支持的功能包括:

  • 连接到SignalR集线器
  • 调用SignalR方法
  • 事件处理程序来处理SignalR通知

它要求通过pip安装以下模块:

  • Gevent
  • sseclient
  • websocket客户端

每个链接的用法示例:

#create a connection
connection = Connection(url, session)

#start a connection
connection.start()

#add a handler to process notifications to the connection
connection.handlers += lambda data: print 'Connection: new notification.', data

#get chat hub
chat_hub = connection.hub('chat')

#create new chat message handler
def message_received(message):
    print 'Hub: New message.', message

#receive new chat messages from the hub
chat_hub.client.on('message_received', message_received)

#send a new message to the hub
chat_hub.server.invoke('send_message', 'Hello!')

#do not receive new messages
chat_hub.client.off('message_received', message_received)

#close the connection
connection.close()
Run Code Online (Sandbox Code Playgroud)


dav*_*owl 3

我可以想到几种方法,它们都是理论上的(并且一开始可能是坏主意):

  • IPC - 将 python 应用程序和信号器应用程序托管为不同的进程,并使用某种 ipc 机制(命名管道、tcp 等)以某种方式来回传输信息。
  • 使用 IronPython(这可能不是一个真正的选择)。
  • 将 SignalR 的轻量级版本移植到 python ( https://github.com/davidfowl/SignalR.Lite )。可能不会以相同的方式工作,但也许您不需要所有功能。

或者你可以希望在 python 中找到一个具有类似功能的库。