在python中同时使用WebSocket和I/O串行?

qfe3c7zg  于 2022-11-11  发布在  Python
关注(0)|答案(1)|浏览(171)

我正在努力让我的WebSocket脚本和我的I/O串行脚本一起运行在一个一起。
在我继续之前,请先了解一些基本信息:

  • 我使用的是Windows PC(无法访问Linux PC)
  • 这就是为什么我使用AIOserial库而不是pyserial-asyncio的原因
  • 我没有“超级”的经验与asyncio,所以请善良:)

下面是我的“旧”WebSocket脚本:

from aiohttp import web
import socketio
import aiohttp_cors
import asyncio
import random

# creates a new Async Socket IO Server

sio = socketio.AsyncServer()

# Creates

app = web.Application()
sio.attach(app)

server_is_responding = "Message from the server:"
the_response  = "Hello there!"

async def index(request):
    with open('index.html') as f:
        print("Somebody entered the server from the browser!")
        return web.Response(text=f.read(), content_type='text/html')

@sio.on("android-device")
async def message(sid, data):
    print("message: ", data)
    #return send_message_to_client()

@sio.on('sendTextToServer')
async def message(sid, data):
    print("message: " , data)
    if data == "hei":
        await sio.emit("ServerMessage", {"hehe"})
    if data == "lol":
        await sio.emit("ServerMessage", {"Message from server:": "hehe, funny right?.."})
    else:
        await sio.emit("ServerMessage", {"Message from server:": "Hello There!"})

# We bind our aiohttp endpoint to our app

# router

cors = aiohttp_cors.setup(app)
app.router.add_get('/', index)

# We kick off our server

if __name__ == '__main__':
    web.run_app(app)

下面是我的I/O串行脚本(它工作并读取数据),我正尝试将其与上面的一些WebSocket函数一起使用:

import asyncio
import websockets
import socketio
import aiohttp_cors

import logging

from AIOExtensions.AIOSerial import (AIOSerial, AIOSerialClosedException,
                                     AIOSerialErrorException, AIOSerialNotOpenException)

logging.basicConfig(level=logging.DEBUG)

sio = socketio.AsyncServer()

async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

@sio.on("android-device")
async def message(sid, data):
    print("message: ", data)

async def read_IO_serial():

    try:

        async with AIOSerial('COM8', baudrate=115200, line_mode=True) as aios:

            await asyncio.sleep(100)

            try:
                while True:

                    # read with timeout
                    rcvd = await asyncio.wait_for(aios.read(), timeout=1.0)
                    # print the data received
                    print(f"data received: {rcvd}")

                    if rcvd == b'RF initialized\n':
                        print("CATCHED THIS LINE!")

            except asyncio.TimeoutError:
                print("reception timed out ;-(")

    except AIOSerialNotOpenException:
        print("Unable to open the port!")
        print()
        print("Have you specified the right port number? COM7? COM8?")
    # port fatal error
    except AIOSerialErrorException:
        print("Port error!")
    # port already closed
    except AIOSerialClosedException:
        print("Serial port is closed!")

start_server = websockets.serve(hello, "http://192.168.1.6", 8080)

# sio.attach(start_server) # HOW CAN I ATTACH THIS SO IT CAN BE USED WITH THE SIO FUNCTIONS BELOW?

if start_server:
    print("Server started!")

asyncio.run(read_IO_serial())

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

正如您在我的第一个简单的WebSocket脚本中看到的,我可以使用“sio.attach(app)”,这使得从客户端列出事件成为可能,所以我需要一种方法来替换我当前脚本中的这个“app”。
有人能帮我吗?

wlp8pajw

wlp8pajw1#

我用asyncio.gather()解决了这个问题,我是这样写的:

from aiohttp import web
import socketio
import aiohttp_cors
import asyncio
import random
​
import asyncio as aio
import logging
​
import sys
​

# creates a new Async Socket IO Server

sio = socketio.AsyncServer()

# Creates

app = web.Application()
sio.attach(app)
​
server_is_responding = "Message from the server:"
the_response = "Hello there!"
​
​
async def index(request):
    with open('index.html') as f:
        print("Somebody entered the server from the browser!")
        return web.Response(text=f.read(), content_type='text/html')
​
​
@sio.event
async def join(sid, message):
    sio.enter_room(sid, message['room'])
    await sio.emit('my_response', {'data': 'Entered room: ' + message['room']}, room=sid)
​
​
@sio.on("android-device")
async def message(sid, data):
    print("message: ", data)
​
​
@sio.on("receiveMessageFromServer")
async def message(sid, data):
    print("message: ", data)
    # await asyncio.sleep(1 * random.random())
    return "OKKKK", 123
​
​
​
from AIOExtensions.AIOSerial import (AIOSerial, AIOSerialClosedException,
                                     AIOSerialErrorException, AIOSerialNotOpenException)
​
logging.basicConfig(level=logging.DEBUG)
​
​
async def read_IO_serial():
    try:
​
        async with AIOSerial('COM8', baudrate=115200, line_mode=True) as aios:
            # aios.sp.baudrate = 230400
            # aios.sp.baudrate = 115200
​
            # await aios.write(b"AT\r\n")
​
            # await aios.read()
​
            # await aios.close()
​
            await aio.sleep(100)
​
            try:
                while True:
​
                    # read with timeout
                    rcvd = await aio.wait_for(aios.read(), timeout=1.0)
                    # print the data received
                    print(f"data received: {rcvd}")
​
                    if rcvd == b'RF initialized\n':
                        print("CATCHED THIS LINE!")
​
​
            except aio.TimeoutError:
                print("reception timed out ;-(")
​
    except AIOSerialNotOpenException:
        print("Unable to open the port!")
        print()
        print("Have you specified the right port number? COM7? COM8?")
    # port fatal error
    except AIOSerialErrorException:
        print("Port error!")
    # port already closed
    except AIOSerialClosedException:
        print("Serial port is closed!")
​
​
async def on_startup(app):
    pass
​
​
cors = aiohttp_cors.setup(app)
app.router.add_get('/', index)
​

# We kick off our server

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    group2 = asyncio.gather(read_IO_serial())
    group1 = asyncio.gather(web.run_app(app))
​
    all_groups = asyncio.gather(group1, group2)
​
    results = loop.run_until_complete(all_groups)
​
    # loop.close()
​
    #print(results)

相关问题