Hello ,
this is my WS server test
just listen and wait for message with '29' then send "ok" \ "false" acoording to the request
maybe the sending is wrong ? and I need to make another code to send the data just 1 time ?
Thanks ,
this is my WS server test
just listen and wait for message with '29' then send "ok" \ "false" acoording to the request
import asyncio import websockets import time from datetime import datetime async def hello(websocket, path): while True: print('Got request') task = await websocket.recv() remote_ip = websocket.remote_address print(task) if "29" in task: print('this is a good reading from ' + str(remote_ip)) await websocket.send('Good question!') await websocket.send('Wrong question!') start_server = websockets.serve(hello, "10.0.0.104", 6500) asyncio.get_event_loop().run_until_complete(start_server) # this for startup the websocket server print("server started") asyncio.get_event_loop().run_forever()and this is the client code:
import asyncio import websockets import time Message = "test 29" async def hello(): try: async with websockets.connect(uri1) as websocket: await websocket.send(Message) replay = await websocket.recv() print (replay) except Exception as e: print (e) print ('problem sending') asyncio.get_event_loop().run_until_complate(hello())on the client side I get the correct messages
Good question!but on the server side I get this
server started Got request Test 29 this is just reading!('10.0.0.104', 45966) Got request Error in connection handler Traceback (most recent call last): File "/home/pi/.local/lib/python3.7/site-packages/websockets/server.py", line 191, in handler await self.ws_handler(self, path) File "/home/pi/Documents/WebSocketServerReadData.py", line 11, in hello task = await websocket.recv() File "/home/pi/.local/lib/python3.7/site-packages/websockets/protocol.py", line 509, in recv await self.ensure_open() File "/home/pi/.local/lib/python3.7/site-packages/websockets/protocol.py", line 812, in ensure_open raise self.connection_closed_exc() websockets.exceptions.ConnectionClosedOK: code = 1000 (OK), no reasonwhat is wrong?
maybe the sending is wrong ? and I need to make another code to send the data just 1 time ?
Thanks ,