Spring Boot jetty websocket 客户端注释不起作用

Springboot jetty web socket client annotation doesn't work

我用java springboot开发了web socket客户端。 我在 build.gradle

上添加以下行时使用了 jetty websocket 库
compile group: 'org.eclipse.jetty.websocket', name: 'websocket-client', version: '9.4.12.v20180830'

我制作了如下所示的网络套接字事件处理程序 - SimpleEchoSocket.java。

package com.iimp.pom.socket;

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@WebSocket(maxTextMessageSize = 64 * 1024)
public class SimpleEchoSocket{

    private final CountDownLatch closeLatch;

    @SuppressWarnings("unused")
    private Session session;

    public SimpleEchoSocket(){
        this.closeLatch = new CountDownLatch(1);
    }

    public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException{
        return this.closeLatch.await(duration,unit);
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason){
        System.out.printf("Connection closed: %d - %s%n",statusCode,reason);
        this.session = null;
        this.closeLatch.countDown(); // trigger latch
    }

    @OnWebSocketConnect
    public void onConnect(Session session){
        System.out.printf("Got connect: %s%n",session);
        this.session = session;
        try{
//            CommonGlobalVariable.webSocketSession = session;
            Future<Void> fut;
            fut = session.getRemote().sendStringByFuture("Hello");
            fut.get(2,TimeUnit.SECONDS); // wait for send to complete.
        }catch (Throwable t){
            t.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessage(String msg){
        System.out.printf("Got msg: %s%n",msg);
    }
}



此外,我做了如下连接部分。

String destUri = "ws://"+body.get("host").toString()+"/va?api-key="+body.get("apiKey").toString()+"&plate=img";

            WebSocketClient client = new WebSocketClient();
            SimpleEchoSocket socket = new SimpleEchoSocket();

            client.start();
            URI echoUri = new URI(destUri);
            ClientUpgradeRequest requestws = new ClientUpgradeRequest();
            requestws.setSubProtocols("va-metadata");

            client.connect(socket,echoUri,requestws);
            System.out.printf("Connecting to : %s%n",echoUri);

因此,我猜想已成功连接到网络套接字服务器,因为我从服务器找到了日志。 然而@OnWebSocketConnect 注解方法中的代码并没有被执行。

如何运行 SimpleEchoSocket.java中的代码?

我参考了Joakim的评论想通了

我更改了 SimpleEchoSocket.java 如下所示。

 @OnWebSocketConnect
    public void onConnect(Session session){
        System.out.printf("Got connect: %s%n",session);
        this.session = session;
        try{
//            CommonGlobalVariable.webSocketSession = session;
//            Future<Void> fut;
//            fut = session.getRemote().sendStringByFuture("Hello");
//            fut.get(2,TimeUnit.SECONDS); // wait for send to complete.
        }catch (Throwable t){
            t.printStackTrace();
        }
    }

    @OnWebSocketMessage
    public void onMessageString(Session session, String msg){
        System.out.println("getRemoteAddress1:"+session.getRemoteAddress());
        String vaHost = session.getRemoteAddress().toString().replaceAll("/", "");
        System.out.println();
        System.out.printf("Got msg: %s%n",msg);
    }

    @OnWebSocketMessage
    public void onMessageBuffer(Session session, byte[] byteArray, int offset, int length) throws IOException {
        System.out.println("onMessageBuffer");
        System.out.println("getRemoteAddress2:"+session.getRemoteAddress());
        FileUtils.writeByteArrayToFile(new File("C:/files/ws/"+System.nanoTime()+".jpg"), byteArray);
    }

谢谢你,Joakim!