TakahikoKawasaki / nv-websocket-client

High-quality WebSocket client implementation in Java.
Apache License 2.0
2.03k stars 292 forks source link

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

Open gdaneluti opened 3 years ago

gdaneluti commented 3 years ago

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 commented 2 years ago

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)
}