So senden und empfangen Sie mit Python Websocket -Nachrichten und empfangen Sie Nachrichten

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: So senden und empfangen Sie mit Python Websocket -Nachrichten und empfangen Sie Nachrichten

by Anonymous » 04 Mar 2025, 14:00

Wie senden und empfangen Sie Nachrichten mit Pythons Asyncio und der WebSockets -Bibliothek?
Ich verwende Django -Kanäle als Socket -Server. Im Grunde versuche ich also, diesen Socket -Server zu senden und zu empfangen.#!/usr/bin/python3

async def main():
uri = f"{wsPrefix}stream/{machineSerial}/?{token}"
async with websockets.connect(uri, ping_interval = None) as websocket:
try:
cap = acapture.open(0)
except:
asyncio.sleep(1)
cap = acapture.open(0)
while True:
check,frame = cap.read()
if not websocket.open:
print("### reconnecting ###")
await websockets.connect(uri,ping_interval = None)
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
check, jpeg = cv2.imencode('.jpg', frame)
frame = jpeg.tobytes()
frame = bytearray(frame)
await websocket.send(frame)
await asyncio.sleep(0.05)

if __name__ == "__main__":
import websockets
import asyncio
import acapture
import cv2
from deviceSpecificVals import machineSerial, token
import deviceSpecificVals.url

urlPrefix = deviceSpecificVals.url
wsPrefix = deviceSpecificVals.wsPrefix

asyncio.run(main())
< /code>
Aber wie können Sie hier eine weitere asynchronisierende Funktion hinzufügen, um immer auf eine Nachricht zu hören? Ich möchte#!/usr/bin/python3

async def connect():
uri = f"{wsPrefix}stream/{machineSerial}/?{token}"
async with websockets.connect(uri, ping_interval = None) as websocket:
return websocket

async def rcv(websocket):
while True:
msg = await websocket.recv()
print(f"< {msg}")

async def send(websocket):
uri = f"{wsPrefix}stream/{machineSerial}/?{token}"
try:
cap = acapture.open(0)
except:
asyncio.sleep(1)
cap = acapture.open(0)
while True:
check,frame = cap.read()
if not websocket.open:
print("### reconnecting ###")
await websockets.connect(uri,ping_interval = None)
if check:
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
check, jpeg = cv2.imencode('.jpg', frame)
frame = jpeg.tobytes()
frame = bytearray(frame)
await websocket.send(frame)
await asyncio.sleep(0.05)

async def main():
websocket = await connect()
asyncio.ensure_future(send(websocket))
asyncio.ensure_future(rcv(websocket))
await asyncio.sleep(100000)

if __name__ == "__main__":
import websockets
import asyncio
import acapture
import cv2
from deviceSpecificVals import machineSerial, token
import deviceSpecificVals.url

urlPrefix = deviceSpecificVals.url
wsPrefix = deviceSpecificVals.wsPrefix

asyncio.run(main())
< /code>
Dies wird nicht zu WebSocket hergestellt. Es ist fast so, als würde die Verbindungsfunktion das WebSocket -Objekt nicht zurückgeben. Ich bin auch sehr neu in Async und versuche zu verstehen, was hier passiert

Top