如何使用Python Push websocket客户端,教程?尝试推杆但没有成功(示例不完整?)



我是websocket和push技术的新手。我必须开发一个基于Python(基于RPI(的推送客户端来与服务器通信。

推送服务器基于push Laravel技术(push+websocket(。通过https+登录/密码注册来保护对serveur的访问。

我可以使用htpps连接/注册到服务器,,但我不知道如何订阅服务器的频道

我在这里找到了一个python推送客户端的例子https://github.com/deepbrook/Pysher还有这里的问题:在Pusher客户端中接收事件但我认为这些示例并不完整,因为在这些示例中,代码中没有指定WS服务器。。。

以下是我所做的(我删除了机密信息并替换为…(:

global pusher
LOGIN_URL = ...
URL = ...
EMAIL= ...
PASSWORD= ...
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
client = requests.session()
response=client.get(LOGIN_URL)
soup = BeautifulSoup(response.text, 'html.parser')
csrftoken = soup.select_one('meta[name="csrftoken"]')['content']
login_data = dict(_token=csrftoken,email=EMAIL, password=PASSWORD, )
r = client.post(URL, data=login_data, headers=dict(Referer=URL))
ws = create_connection("wss://......../app/...key...?protocol=7&client=js&version=7.0.0&flash=false")
root = logging.getLogger()
root.setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
root.addHandler(ch)
def  my_func(*args, **kwargs):
print("processing Args:", args)
print("processing Kwargs:", kwargs)

def connect_handler(data):
channel = pusher.subscribe('presence-chat')
channel.bind('App\Events\MessageSent', my_func)
appkey='....'
PUSHER_APP_ID='..'
PUSHER_APP_KEY='...'
PUSHER_APP_SECRET='...'
PUSHER_APP_CLUSTER='...'
pusher = pusherclient.Pusher(PUSHER_APP_ID, PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_CLUSTER)
pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()
while True:
# Do other things in the meantime here...
time.sleep(1)

但我在执行时会出现以下错误:

error from callback <bound method Connection._on_open of <Connection(Thread-1, started daemon 14552)>>: _on_open() missing 1 required positional argument: 'ws'
error from callback <bound method Connection._on_message of <Connection(Thread-1, started daemon 14552)>>: _on_message() missing 1 required positional argument: 'message'
error from callback <bound method Connection._on_close of <Connection(Thread-1, started daemon 14552)>>: _on_close() missing 1 required positional argument: 'ws'

可能是因为我没有为pusher指定WS服务器地址,但我不知道如何做到这一点(令人惊讶的是,我在上面发现的例子也没有指定这样的WS地址(。

另一种可能性是只使用python-websocket-lib(因此没有pusher-lib(直接编码push对话框,如下所示:

client = requests.session()
response=client.get(LOGIN_URL)
soup = BeautifulSoup(response.text, 'html.parser')
csrftoken = soup.select_one('meta[name="csrf-token"]')['content']
login_data = dict(_token=csrftoken,email=EMAIL, password=PASSWORD, )
r = client.post(URL, data=login_data, headers=dict(Referer=URL))
async def hello():
async with websockets.connect(
URI, ssl=True #ssl_context
) as websocket:
name = input("Message to send ? ")
await websocket.send(name)
print(f"Sent message : {name}")
greeting = await websocket.recv()
print(f"Received : {greeting}")
asyncio.get_event_loop().run_until_complete(hello())

有了这个代码,我就可以建立一个连接:

"event":"pusher:connection_established","data":"{"socket_id":"54346.8202313328","activity_timeout":30}"}

但我当时不知道如何订阅频道。我想我必须使用json等形成一个命令,但需要一些示例。。。

任何帮助、指点或其他什么都将不胜感激,因为我现在完全迷路了。

祝2021年一切顺利,不可能是最糟糕的一年!当做Atmos

最后我解决了这个问题:推送服务器地址被硬编码在类Pusher文件中"pusherclient/init.py";,host=";wss:ws。。。。com";

不幸的是,我仍然有一些错误:

error from callback <bound method Connection._on_error of <Connection(Thread-1, started daemon 8332)>>: _on_error() missing 1 required positional argument: 'error'
error from callback <bound method Connection._on_close of <Connection(Thread-1, started daemon 8332)>>: _on_close() missing 1 required positional argument: 'ws'

最新更新