TakahikoKawasaki / nv-websocket-client

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

onTextMessage never called #125

Closed remusmp closed 7 years ago

remusmp commented 7 years ago

Thank you very much for this package. I think I'm overlooking something really simple to get things working. I'm able to connect to my server as onConnected callback is triggered. I'm also able to send messages to my server as I can see them arriving on the server side. However when I send a message from my server the onTextMessage callback is not triggered :(

I'm extracting some lines of code showing how I've set things up in my Android app:

// These lines of code are from my App class
// I have a self-signed certificate
mWebSocketFactory = new WebSocketFactory();
mWebSocketFactory.setSSLContext(mSSLContext);
mWebSocketFactory.setVerifyHostname(false);

// These lines of code are from an Activity class (that also contains some fragments)
mWS = mApp.mWebSocketFactory.createSocket(path, 5000);
mWS.addExtension(WebSocketExtension.PERMESSAGE_DEFLATE);
mWS.addListener(new WebSocketAdapter() {
  @Override
    public void onTextMessage(WebSocket websocket, String text) throws Exception {
      Log.d(mApp.DEBUG_TAG, text);
    }
  @Override
  public void onConnected(WebSocket ws, Map<String, List<String>> headers) {
    List<WebSocketExtension> extensions = ws.getAgreedExtensions();
    Log.d(mApp.DEBUG_TAG, "Connected");
    mWS.sendText("Hello world!", true);
    Log.d(mApp.DEBUG_TAG, extensions.get(0).getName());
  }
});
mWS.connectAsynchronously();

extensions are null, which I find weird because I've set the per message deflate extension. onConnected is called, no problems here (I can also see the connection on the server side). Sending messages to the server also works. The issue is that onTextMessage is never called when I send messages from the server :(

Have I missed some configuration of the websocket?

MinnDevelopment commented 7 years ago

There is an alternative method onBinaryMessage which is called when receiving a binary message instead of a text message. Is it possible that this is the case here?

remusmp commented 7 years ago

@MinnDevelopment Just tried that out too:


@Override
public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
  Log.d(mApp.DEBUG_TAG, "Binary message");
}

It doesn't get triggered :( It's weird because I can send data from the Android app to the server through the web socket (one way communication).

remusmp commented 7 years ago

It works! I was sending from the server in a wrong way (django-channels). I fixed that and everything works now as expected! Many thanks!