Java WebSocket客户端与Spring集成(依赖注入),无’CONNECT’消息。

huangapple go评论71阅读模式
英文:

Java WebSocket client with Spring integration (DI) and no 'CONNECT' message

问题

以下是翻译好的内容:

我想创建一个Java 8的WebSocket客户端应用程序,与Spring集成,并且不发送初始连接消息,或者该消息可以进行自定义。

我已经尝试过使用tyrus 1.17,但是ClientManager.createClient()会创建我注释的类@ClientEndpoint的新实例,我找不到方法来使用Spring创建的其他实例。

我还尝试过使用spring-websocket 5.2.9.RELEASE和spring-stomp 5.3.2.RELEASE,但它会发送预定义的'CONNECT'消息,这会破坏与服务器的集成,因为服务器无法理解它。

如果有人可以提供任何指导,无论是来自我之前未见过的谷歌示例,还是支持所有这些的实现/示例,都将非常有帮助。

英文:

I would like to create a java 8 WebSocket client application, that integrates with Spring and sends no initial connect message or the message can be customized.

I have already tried it with tyrus 1.17, but ClientManager.createClient() creates new instances of my annotated classes @ClientEndpoint and I found no way to use the other instance that Spring created.

I have also tried it with spring-websocket 5.2.9.RELEASE and spring-stomp 5.3.2.RELEASE, but it sends its pre-defined 'CONNECT' message that breaks my integration with the server as it does not understand it.

If someone could help with any guidance on either some examples from google, that I have not seen previously, or with implementations/examples that supports all of it, would be great.

答案1

得分: 0

jetty-websocket-client-api支持两种方式。

<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-client</artifactId>
    <version>9.4.32.v20200930</version>
</dependency>

在Spring Bean上添加@org.eclipse.jetty.websocket.api.annotations.WebSocket注解,然后创建四个带有注解的方法来处理不同的事件:

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.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;

@OnWebSocketConnect public void onConnect(Session session) { }
@OnWebSocketMessage public void onMessage(String text) { }
@OnWebSocketClose public void onClose(int statusCode, String reason) { }
@OnWebSocketError public void onError(Throwable cause) { }

然后连接并配置WebSocket客户端:

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

@Override
public void afterSingletonsInstantiated() {
    Object mySpringBeanForWebsocket = this;
    WebSocketClient client = new WebSocketClient();
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    client.start();
    client.connect(mySpringBeanForWebsocket, new URI("wss://my.secure.websocket.server.address.com"), request);
}

然后在api调用onConnect方法时,可以立即发送自定义初始消息:

@OnWebSocketConnect
public void onConnect(Session session) {
    session.getRemote().sendString("Here comes my first message");
}
英文:

jetty-websocket-client-api supports both.

<dependency>
    <groupId>org.eclipse.jetty.websocket</groupId>
    <artifactId>websocket-client</artifactId>
    <version>9.4.32.v20200930</version>
</dependency>

Add @org.eclipse.jetty.websocket.api.annotations.WebSocket annotation to the spring bean then create four annotated methods to handle the different events:

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.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;

@OnWebSocketConnect public void onConnect(Session session) { }
@OnWebSocketMessage public void onMessage(String text) { }
@OnWebSocketClose public void onClose(int statusCode, String reason) { }
@OnWebSocketError public void onError(Throwable cause) { }

Then wire up the WebSocket client and connect

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

@Override
public void afterSingletonsInstantiated() {
    Object mySpringBeanForWebsocket = this;
    WebSocketClient client = new WebSocketClient();
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    client.start();
    client.connect(mySpringBeanForWebsocket, new URI("wss://my.secure.websocket.server.address.com"), request);
}

Then the custom initial message can be sent as soon as the api calls the onConnect method

@OnWebSocketConnect
public void onConnect(Session session) {
    session.getRemote().sendString("Here comes my first message");
}

huangapple
  • 本文由 发表于 2020年10月10日 06:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定