Я использую каналы Django в качестве сервера сокетов. Итак, по сути, я пытаюсь отправлять и получать сообщения с этого сокет-сервера.
Я могу легко отправлять сообщения на свой веб-сервер, используя:
Код: Выделить всё
#!/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())
Я пробовал:
Код: Выделить всё
#!/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())
Подробнее здесь: https://stackoverflow.com/questions/677 ... ith-python
Мобильная версия