java websocket客户端不接收来自服务器的任何消息

时间:2019-03-25 22:17:06

标签: java websocket

我有这个 websocket客户端,它连接到服务器。

@ClientEndpoint
class WSClient {
    private static Object waitLock = new Object();

    @OnMessage
    public void onMessage(String message) throws IOException {
        System.out.println("Received msg: " + message);
    }

    private static void wait4TerminateSignal() {
        synchronized (waitLock) {
            try {
                waitLock.wait();
            } catch (InterruptedException e) {
            }
        }
    }

    public static void main(String[] args) {
        WebSocketContainer container = null; //
        Session session = null;
        try {
            //Tyrus is plugged via ServiceLoader API. See notes above
            container = ContainerProvider.getWebSocketContainer();
            //WS1 is the context-root of my web.app
            //ratesrv is the  path given in the ServerEndPoint annotation on server implementation
            session = container.connectToServer(WSClient.class, URI.create("ws://localhost:8080/websocket"));
            // message of bytes
            session.getAsyncRemote().sendBinary(ByteBuffer.wrap(bytes));
            wait4TerminateSignal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

然后,它发送一条消息,服务器接受并继续该过程。但是,服务器会发回应由 @OnMessage 带注释的方法处理的响应,但是没有响应。服务器代码使用 Spring 并正常工作-它将邮件发送给其他客户端。

感谢帮助!

0 个答案:

没有答案