可以增加在项目作为websocket客户端的情况下,同时连接多个服务端的功能吗 #265
zhu-weixin
started this conversation in
Ideas
Replies: 2 comments
-
这个是我写的的连接多个websocket服务器的配置@Data
@Configuration
@ConfigurationProperties(prefix = "shiro.ws.client")
public class PluralBotClientConfig implements WebSocketConfigurer {
/**
* 是否启用多个正向 Websocket 连接
*/
private Boolean enable = false;
/**
* 正向 Websocket 连接列表
*/
private List<WebSocketClientProperties> clientProperties;
@Autowired
private ScheduledTask scheduledTask;
@Autowired
private WebSocketProperties wsProp;
@Autowired
private WebSocketClientHandler webSocketClientHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
if (Boolean.TRUE.equals(enable)) {
createWebsocketClients();
}
}
private void createWebsocketClients() {
for (WebSocketClientProperties clientProperty : clientProperties) {
if (clientProperty.getEnable()) {
StandardWebSocketClient client = new StandardWebSocketClient();
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
if (!Objects.equals(wsProp.getAccessToken(), "")) headers.add("Authorization", wsProp.getAccessToken());
WebSocketConnectionManager manager = new WebSocketConnectionManager(client, webSocketClientHandler, clientProperty.getUrl());
manager.setHeaders(headers);
manager.setAutoStartup(true);
scheduledTask.executor().scheduleAtFixedRate(() -> {
if (!manager.isConnected()) {
manager.startInternal();
}
}, 0, clientProperty.getReconnectInterval(), TimeUnit.SECONDS);
}
}
}
} yml配置
但是连接间的相互通信和连接使用的插件管理还没有头绪目前只是大概的想法,但是可行性还未知:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
感觉可能会有问题,不过我最近都比较忙,短时间内可能不太行。 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
我自己写了配置可以实现,但是我自己配置的话,会很不规范。请问作者大大有考虑在框架中集成相关功能吗
Beta Was this translation helpful? Give feedback.
All reactions