Dec-29-2022, 04:29 PM
My mission is to create a websocket server that send signal to any client connected to the websocket, the signal should be sent only when they is a specific event.
1. So I have a function called calc_totential_buys(), this function is what is returning the signal that i want to send to the client via a websocket.
If the signal returned from calc_totential_buys() is None then that is considered as no event and I only want to send to the client when they is an event
the verify_user() function is a function that takes in a user unique license key and the computer serial number as machine_id as it params
and it will check the database to very if the client that is connected to the websocket is whitelisted by sending a dict of some values to the client machine and the client websocket side will handle the verification
1. So I have a function called calc_totential_buys(), this function is what is returning the signal that i want to send to the client via a websocket.
If the signal returned from calc_totential_buys() is None then that is considered as no event and I only want to send to the client when they is an event
async def calc_totential_buys(): # This code is not my actual code, it is just for demostration purpose database_return_value = mydb_data() if database_return_value != []: return database_return_value else: return None2. then I also have my server side websocket code
the verify_user() function is a function that takes in a user unique license key and the computer serial number as machine_id as it params
and it will check the database to very if the client that is connected to the websocket is whitelisted by sending a dict of some values to the client machine and the client websocket side will handle the verification
import json, websockets, asyncio wsssets = set() async def echo(wss, path): wsssets.add(wss) try: async for message in wss: if "lin=" in message: x = message.replace("lin=","").split("-") license_key = x[0] machine_id = x[1] resp_msg = verify_user(license_key, machine_id, mydb) await wss.send(json.dumps(resp_msg)) else: # here will be the code to handle a message the the client send to the server in ocasion # so that i will know that the last time the client was active pass except websockets.exceptions.ConnectionClosed as err: print(err) start_server = websockets.serve(echo, "localhost", PORT,) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()3. Now the problem I'm facing is:
- If the client is connecting for the first time then the verify_user() function is been call sucessfully and it return the data from the database as needed, but if the same client is connecting for the second time or another client is connecting to the websocket then verify_user() function will still return the same value that it returned on first initialization, meaning it does not fetch the data from the database anymore untill i restart the server websocket.
- I dont even know where and how to call calc_totential_buys() function in the echo() function to send the signal whenever the return value is not None beause the echo() function is runing constantly on where it is looping to check for the connected client message and if I try to call the calc_totential_buys() and use if statement to check if the value is not None then send to the client a message then that line of code will not be reached.