Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_open && ws.send() #980

Open
Dpsley opened this issue Apr 18, 2024 · 5 comments
Open

on_open && ws.send() #980

Dpsley opened this issue Apr 18, 2024 · 5 comments

Comments

@Dpsley
Copy link

Dpsley commented Apr 18, 2024

Hi, I just started working with this recently and I can't understand why I can't send a message after opening a socket using run_forever(), does all the logic have to be in on_open, because so far I get that the socket is already closed

class Connector:
    def on_message(self, ws, message):
        print(data)
        
    def on_open(self, ws):
        print("opened")
        ws.send(json.dumps({"function": "test"}))
    
    def on_error(self, ws, error):
        print(ws)
        print(error)
        
    def on_close(self, ws, close_status_code, close_msg):
        try:
            if self.ws:
                self.ws.close()
        except Exception as ex:
            print(f"Error in close: {ex}")
            
    def __init__(self, ip):
        self.ws = websocket.WebSocketApp(f'ws://{self.ip}:25000/',
                                         on_open=self.on_open,
                                         on_message=self.on_message,
                                         on_error=self.on_error,
                                         on_close=self.on_close)
                                         
    def run_websocket(self):
        websocket.enableTrace(False)
        self.ws.run_forever()
                
socket_holder = Connector("192.168.88.2")
socket_holder.run_websocket()
socket_holder.ws.send_text('test')
@eness-1
Copy link

eness-1 commented Apr 19, 2024

cause the ws.run_forever() method is a blocking method, the program won't continue executing beyond the line self.ws.run_forever(). If you want to send() while run_forever() is running, you may need to run run_forever() in the background as a listener, like this.
`

target_fun = self.ws.run_forever

daemon = threading.Thread(target=target_fun)

daemon.daemon = True

daemon.start()

socket_holder.ws.send_text('test')

`

@Dpsley
Copy link
Author

Dpsley commented Apr 19, 2024

Hi, thanks for the reply. Did I understand correctly?

wws = WMFMachineErrorConnector2("192.168.88.2")
daemon = threading.Thread(target=wws.run_websocket)
daemon.daemon = True
daemon.start()
wws.ws.send_text('test')

@eness-1
Copy link

eness-1 commented Apr 19, 2024

y

@Dpsley
Copy link
Author

Dpsley commented Apr 19, 2024

but i getting a exception about already closed socket before sending a "test" message

@Dpsley
Copy link
Author

Dpsley commented Apr 19, 2024

If i using threads in the on_open function thats working but i would like to sending and using outside this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants