Python Websocket服务器客户端PubSub

问题描述

  1. 我正在从网站获取WebSocket数据,代码python编写,并使用autobahntwistedthreading进行WebSocket处理。 ws.subscribe(<list>)用于订阅不同的项目,ws.unsubscribe(<list>)用于使用on_message()取消订阅显示数据。

  2. 我想创建一个本地服务器,该服务器将服务于第1步中获得的数据(我希望是中继服务器)。我在想的流程:

    a。本地客户端将请求订阅本地服务器。

    b。本地服务器根据从本地客户端收到的订阅/取消订阅请求,将订阅实际服务器(步骤1)。

    c。每当从实际服务器收到任何消息时,本地服务器都会根据订阅向本地客户端发送数据。

第1步代码

from MainServer import MainDataService

mds = MainDataService(
    user_id='user_id',apikey='APIKEY')
)
def on_connect(ws,response):
    # settings subscribed,need to change based on local subscribe request
    ws.subscribe(['demand'])
def on_message(ws,payload,is_binary):
    # getting the message here,that need to relay based on local subscribe
    if is_binary:
        print(_parse_binary(payload))
    else:
        print(_parse_text_message(payload))

mds.on_connect = on_connect
mds.on_message = on_message

mds.connect(threaded=True)

如何实现这一点,使用什么?任何指南/代码都会有所帮助。

编辑1:这可能与crossbar.io,PUB / SUB,中继,WAMP,路由器有关。

编辑2:我做了什么:

启动纵横制路由器:crossbar initcrossbar start

pub.py

from autobahn.asyncio.component import Component
from asyncio import sleep
from autobahn.asyncio.component import run

component = Component(
    transports=u"ws://localhost:8080/ws",realm=u"realm1",)

@component.on_join
async def joined(session,details):
    print("session ready")

    counter = 0
    while True:
        session.publish(u'com.myapp.oncounter',counter)
        counter += 1
        await sleep(1)
        
if __name__ == "__main__":
    run([component])

sub.py

from autobahn.asyncio.component import Component
from autobahn.asyncio.component import run

component = Component(
    transports=u"ws://localhost:8080/ws",)

@component.subscribe(u"com.myapp.oncounter")
def oncounter(count):
    print("event received: {0}",count)
    
if __name__ == "__main__":
    run([component])

pub.pysub.py正常运行。但是,我需要动态编码session.publish,如“步骤2”中所述。这意味着只有subscribedemandoncounter的任何客户端才发布。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...