简体   繁体   中英

Jetty WebSocket Client Doesn't work for Binary Listener

I have a Java app that uses the Jetty WebSocket Client, version 9.x. It works fine for text messages sent from the server, but the binary listener is never invoked. I have a Javascript client implementation which I'm basically duplicating. I'm doing the same exact thing in Javascript that I do in Java, calling the same server. The Javascript works, and the Java doesn't. So I'm thinking that something is not configured properly in Jetty for binary listeners.

For example, the server is sending blob data. I know that in the Javascript client, I can set the binarytype to either arraybuffer or blob. I figured there may be a similar setting required in Jetty, but I've looked all through the API and searched many examples online. There are precious few examples of binary listeners online, and no mention anywhere of setting the binarytype, or any other special setting required to make binary llisteners work.

Here's a consolidated representation of my code. The code is spread throughout various classes, so this is not a stand-alone app, but I think it shows what I'm doing. The server is implemented with libwebsockets.

Client implementation

import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;

client = new WebSocketClient();
client.start();
client.setMaxBinaryMessageBufferSize((int) 500e6);//just to be sure
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols("pipe-data");
client = new SimpleSocket();
client.connect(socket, uri, request);

Socket implementation

@WebSocket
public class SimpleSocket {

   @SuppressWarnings("unused")
   private Session session;

   private SocketHandlerBase handler;

   private boolean connected = false;

   public SimpleSocket(SocketHandlerBase listener) {
       this.handler = listener;
   }

   @OnWebSocketClose
   public void onClose(int statusCode, String reason) {
       this.handler.onClose(statusCode, reason);
       this.connected = false;
   }

   @OnWebSocketConnect
   public void onConnect(Session session) {
       this.handler.onConnect(session);
       this.connected = true;
   }

   //invoked when text messages are sent
   @OnWebSocketMessage
   public void onMessage(String msg) {
       this.handler.onMessage(msg);
   }

    //does not get invoked when binary data is sent
   @OnWebSocketMessage
   public void onMessage(byte buf[], int offset, int length) {
       this.handler.onMessage(buf, offset, length);
   }

   public boolean isConnected() {
       return this.connected;
   }

   public SocketHandlerBase getHandler() {
       return this.handler;
   } 
}

There was a hard to find problem with the server I was calling. A very specific configuration of invocation arguments was causing the binary listener to not be called. Nothing about the Jetty client or WebSockets in general involved here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM