Skip to content

[Question] Re-use WebSocketApp connection and share between onMessage and recv() #562

@lswest

Description

@lswest

Hi,

I've got a bit of a difficult use-case. I'm working on a multithreaded program that needs to utilize a websocket connection to initiate a refresh of settings (stored in module-wide variables). However, I also need to use the websocket connection to request specific information from within other threads. I thought I could solve this by storing the WebSocketApp connection in the global variables, and then trigger/receive the messages with settings.ws.send() and settings.ws.sock.recv(). However, this works exactly once... Any subsequent attempts result in the thread that called the .recv() function to wait endlessly, while the response is handled in the onMessage function.

My best guess is that the settings.ws.sock.recv() responds once because the dispatcher is not yet active, but on subsequent calls directs the responses to onMessage().

So, to sum it up, here are the questions:

  1. Is it possible to manually receive a response outside of on_message when running a websocket.WebSocketApp?
  2. Would this approach be possible if I wrote my own class or stored the actual create_connection in a variable? I don't know enough about websockets to judge whether what I'm doing is actually possible/intended with a websocket connection.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions