首页 > 解决方案 > 在 python 中同时使用 websocket 和 I/O 串行?

问题描述

我正在努力让我的 websocket 脚本和我的 I/O 串行脚本一起运行。

在我继续之前只是一些基本信息:

这是我的“旧” 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)”,它可以列出来自客户端的事件,所以我需要一种方法来替换我当前脚本上的这个“应用程序”。

有人可以帮我解决这个问题吗?

标签: pythonwebsocketiopython-asyncio

解决方案


编辑:解决了!!

我使用 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)

推荐阅读