我有这个 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 并正常工作-它将邮件发送给其他客户端。
感谢帮助!