websockets server loop? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: websockets server loop? (/thread-31609.html) |
websockets server loop? - korenron - Dec-22-2020 Hello on my de vice there is a sensor that read data all the time I want to create a web socket , that when someone enter it he will get the data everytime it change (I did it in the past in JAVA) this is what I have doen: import os import time import websockets import asyncio async def DataServer(websocket , path): message = bus.recv() # print (message) PID = message.arbitration_id dlc= message.dlc MyData = message.data MSG_Time = message.timestamp #print(str(MyData)) New_Data = (bytes(MyData).hex()).upper() New_Data = ' '.join(New_Data[i:i+2] for i in range(0,len(New_Data),2)) #print(New_Data) PID_Hex = str(hex(PID)) print (str(MSG_Time) + ' Hex PID is - ' + PID_Hex) if PID_Hex == hex(0x48c): print('this is Door 1') print ('I need bytes 1 , bit 2,1') door1 = New_Data[0:2] print(door1) elif PID_Hex == hex(0x48d): print('this is Door 2') print ('I need bytes 1 , bit 2,1') door2 = New_Data[0:2] print(door2) result = getBinarybits(door2,0,2) print('you need this ' + result) else: print("I don't know this PID!") print ('done') SendData = (door1 + "!" + door2) await websocket.send(SendData) start_server = websockets.serve(DataServer, '10.0.0.100', 8765) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever() print ('WebSocket Server Start!') # I don;t see this messagebut it doesn't read my the data all the time it's seem that it only read the data after I open web socket to him what did I do wrong ? I want the main will be the reading of the sensor Thanks , RE: websockets server loop? - korenron - Dec-23-2020 I have also try to run this code: import asyncio import websockets import time from datetime import datetime async def hello(websocket, path): await websocket.send(TS) print('going to start') start_server = websockets.serve(hello, "10.0.0.188", 8765) print('Start') asyncio.get_event_loop().run_until_complete(start_server) print('I see you') while True: TS = datetime.now().strftime('%d-%m-%y %H-%M-%S-%f') print('time is - ' + str(TS)) time.sleep(0.251)here I can see the time printing , but when I open a websocket to the server , the client is waiting for something without any reply . the server is keep printing the time so I guess I'm close , but what am I missing? Thanks, |