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

How can I have a listener for each sendText Method? #233

Open
gdaneluti opened this issue Jun 11, 2021 · 1 comment
Open

How can I have a listener for each sendText Method? #233

gdaneluti opened this issue Jun 11, 2021 · 1 comment

Comments

@gdaneluti
Copy link

gdaneluti commented Jun 11, 2021

am I able to bind different listeners for each sendText method that I call?

For example:
I have one call to authenticate my application: websocket.sendText("auth request")
I have another one to request some information: websocket.sendText("request some information").

I am receiving both responses in the same listener but I need to receive each one in a specific listener then I can handle each one differently.

Is that possible? I will leave peace of my code here:

WebSocket ws = new WebSocketFactory().createSocket(uri);
        ws.addListener(new WebSocketAdapter() {
            @Override
            public void onTextMessage(WebSocket websocket, String message) throws Exception {
                System.out.println("message: " + message);
            }
        });
        WebSocket session = ws.connect();
        if (session.isOpen()) {
            //Auth request
            session.sendText("auth");
            //Request to get something else
            session.sendText("some thing else");
        }
@ivanov199311
Copy link

No.
This is usually done by sending response type and other metadata in response message.
Like this, for example:

if(response.type == "auth_ok")
{
    subscribeOnDataFlow();
} 
else if(response.type == "command_response")
{
   val commandId = response.commandId
   val commandHandler = handlers.get(commandId)
   val handler.handle(response)
}

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