1

我正在尝试通过 websocket 发送一个 base64 编码的字符串(从图像转换而来),这对我来说使用 javascript 客户端工作得很好。

但是,如果我改用 java 客户端 (@clientEndPoint),onMessage 函数会被触发一次,并且 websocket 连接会立即关闭。没有错误。

同样,我尝试从服务器发送纯字符串,并且 java 客户端正常运行。但只是不适用于 base64 编码的字符串。

我正在使用 Tomcat 7.0.69,以下是代码片段:

服务器端点:

if (imageToBeSend != null) {
   encodedImage = new sun.misc.BASE64Encoder().encode(imageToBeSend);
   session.getBasicRemote().sendText(encodedImage);
   }

Java客户端:

@ClientEndpoint
public class SomeClass {
    CountDownLatch latch = new CountDownLatch(1);
    private Session session;
    String msg;

    @OnOpen
    public void open(Session session) {
        this.session = session; 
        //some stuff
    }

    @OnClose
    public void close(Session session) {
        System.out.println("Closing the connection");
    }

    @OnError
    public void onError(Throwable error) {
        System.out.println("!!!ERROR!!!!!" + error);
        error.printStackTrace();
    }

    @OnMessage
    public void handleMessage(String message, Session session) throws IOException {
     System.out.println("got the json string  "+message.length());
     // more stuff
    }

这里调用了一次句柄消息,并打印了相应的 sysout,然后立即调用 onClose。onError 永远不会被调用。

4

0 回答 0