I'm trying to create nested functions in my WebSocket so I can send multiple different messages in the TCP conn. In addition, I'm trying to figure out how to alter the response so that it won't return a value if the response is not none.
import websocket
class Opcode:
CONNECTED = '40{"sid"'
class Server:
'''
This class is our middleman between us and atshop,
and it handles everything related to websockets.
'''
def __init__(self):
self._connected = False
self._websocket = self.create_websocket()
self.run()
def create_websocket(self):
return websocket.WebSocketApp(
url='wss://websocket.net',
header={
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.7',
'Cookie': '', #some auth cookie
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36'
},
on_open=lambda ws: self.on_open(ws),
on_message=lambda ws, msg: self.on_message(ws, msg),
on_error=lambda ws, msg: self.on_error(ws, msg),
on_close=lambda ws, close_code, close_msg: self.on_close(ws, close_code, close_msg)
)
def on_open(self, ws):
self._websocket.send('40') #inn con
def on_close(self, ws, close_code, close_msg): #conn close
print('Connection is closing.')
def on_error(self, ws, error):
raise Exception(error)
def on_message(self, ws, message):
def send_message(sentMessage, response):
if Opcode.CONNECTED in message:
self._websocket.send(sentMessage)
if response in message:
return(message)
print(send_message())
def run(self):
self._websocket.run_forever(
ping_interval=30,
ping_timeout=10
)
if __name__ == '__main__':
Server = Server()
My problem is that this will send a 2 - 5 responses of None then return the "response" value then countinously return a None value.
What I tried to do:
def on_message(self, ws, message):
def send_message(sentMessage, response):
while True:
if Opcode.CONNECTED in message:
self._websocket.send(sentMessage)
if response in message:
return(message)
print(send_message())
``` <- this code does not work.