tomcat实例可以支持的最大并发websocket连接数是多少?我们希望在任何给定时间提供20000个连接.支持加载的tomcat实例的建议数量是多少?
我有一个Spring应用程序,它通过Spring WebSocket异步发送消息给另一台服务器.但是对于特定情况我需要同步发送消息,我应该继续使用来自服务器的传入响应的过程.
我不想仅为此进程进行HTTP调用,因为已经有一个开放的TCP连接,我想使用它.
例如,在Tyrus WebSocket实现中,可以通过同步或异步发送消息
session.getBasicRemote().sendText(message);
session.getAsyncRemote().sendText(message);
Run Code Online (Sandbox Code Playgroud)
相关的Tyrus文档链接.
顺便说一下,我没有sub-protocol像Spring WebSocket那样使用STOMP.
我有点厌倦了在 Chrome 开发控制台中看到“连接到未定义的服务器”。如何为我的服务器命名?
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/liarsDice").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config){
config.enableSimpleBroker("/topic", "/queue");
config.setApplicationDestinationPrefixes("/app");
}
}
Run Code Online (Sandbox Code Playgroud) 使用SockJS Java客户端,我正在尝试连接到Spring sockjs服务器,并且收到错误消息209(无标题)(错误消息为1009)。Javascript库工作正常。
Transport closed with CloseStatus[code=1009, reason=The decoded text message was too big
for the output buffer and the endpoint does not support partial messages] in WebSocketClientSockJsSession
[id='9fa30eb453e14a8c8612e1064640646a, url=ws://127.0.0.1:8083/user]
Run Code Online (Sandbox Code Playgroud)
我在服务器上有几个配置类(目前我不知道是否配置这些东西的次数超过了必要):
@Configuration
@EnableWebSocket
public class WebSocketTransportConfig implements WebSocketConfigurer {
// Important web socket setup. If big message is coming through, it may overflow the buffer and this will lead in disconnect.
// All messages that are coming through normally (including snapshots) must be order of magnitude smaller, or connection will …Run Code Online (Sandbox Code Playgroud) 我正在构建一个小型 websocket 项目并由 JWT 令牌机制保护,我想将 websocket 会话存储在 redis 而不是本地内存中。
@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
registry
.addEndpoint("/hello")
.addInterceptors(new HttpSessionHandshakeInterceptor() {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession();
attributes.put("sessionId", session.getId());
}
return true;
}
})
.withSockJS()
.setSessionCookieNeeded(true);
}
Run Code Online (Sandbox Code Playgroud)
我在我的 redis 配置中使用 @EnableRedisHttpSession 并且上面的源代码安全地将 http 会话存储在 redis 中,但即使我刷新了我的 redis 数据库,我仍然可以向我连接的客户端发送和接收我的消息。
我的猜测是 HttpSession 与 WebSocket 会话不同。如果您使用基于令牌的身份验证,我应该如何“智能地”要求 spring 在 redis 中存储和维护与 websocket …
我正在尝试将自定义标头添加到客户端在第一次连接时收到的 STOMP 'CREATED' 消息中。这是使用 STOMP JavaScript 连接到 WebSocket 的函数:
function connect() {
socket = new SockJS('/chat');
stompClient = Stomp.over(socket);
stompClient.connect('', '', function(frame) {
whoami = frame.headers['user-name'];
console.log(frame);
stompClient.subscribe('/user/queue/messages', function(message) {
console.log("MESSAGE RECEIVED:");
console.log(message);
showMessage(JSON.parse(message.body));
});
stompClient.subscribe('/topic/active', function(activeMembers) {
showActive(activeMembers);
});
});
}
Run Code Online (Sandbox Code Playgroud)
此函数将以下内容打印到浏览器的控制台:
body: ""
command: "CONNECTED"
headers: Object
heart-beat: "0,0"
user-name: "someuser"
version: "1.1"
Run Code Online (Sandbox Code Playgroud)
我想添加自定义标题,因此输出必须如下所示:
body: ""
command: "CONNECTED"
headers: Object
heart-beat: "0,0"
user-name: "someuser"
version: "1.1"
custom-header: "foo"
Run Code Online (Sandbox Code Playgroud)
我的 Spring Boot 应用程序中有以下 WebSocket 配置。
WebSocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public …Run Code Online (Sandbox Code Playgroud) 问题
有没有办法全局处理MessageDeliveryExceptionSpring WebSocket 模块中由错误(通常权限不足)引起的Spring Messaging ?
用例
我已经通过 STOMP 实现了 Spring WebSockets 以支持我的 web 应用程序中的 ws 连接。为了保护 websocket 端点,我创建了授权用户在 STOMP CONNECT 时间启动 STOMP 会话的拦截器(如 Spring 文档中 22.4.11 部分的建议):
@Component
public class StompMessagingInterceptor extends ChannelInterceptorAdapter {
// Some code not important to the problem
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor headerAccessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
switch (headerAccessor.getCommand()) {
// Authenticate STOMP session on CONNECT using jwt token passed as a STOMP login header - it's …Run Code Online (Sandbox Code Playgroud) java spring spring-security spring-messaging spring-websocket
我正在尝试结合 Spring-Security 来了解有关 Spring-Websocket 的更多信息,并且正在尝试Spring 文档中的示例。
在创建我的课程WebSecurityConfig并从AbstractWebSocketMessageBrokerConfigurer我那里扩展后,我被告知AbstractWebSocketMessageBrokerConfigurer已弃用。
我试图找出是否有其他方法可以将 Spring-Security 与 Spring-Websocket 结合使用,但找不到相关内容。
所以,我的问题是我应该继续使用AbstractWebSocketMessageBrokerConfigurer还是有另一种方法将 Spring-Security 与 Spring-Websocket 结合起来?
这是我在测试项目中实现的示例。它似乎对你有用,但在AbstractWebSocketMessageBrokerConfigurer弃用之前不应该有 Spring 的替代方案吗?
@Configuration
public class WebSocketSecurityConfig extends AbstractWebSocketMessageBrokerConfigurer {
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.simpDestMatchers("/user/*").authenticated();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有 Spring-Security/Session 和 Spring-Websocket 的 Spring Boot 2.2 MVC 应用程序。
它被配置为仅在经过身份验证时允许 websocket 连接。
以下是在单元测试中创建 Websocket 客户端的推荐方法。(至少我在 spring 文档中看到过这个)
private StompSession createWebsocket() throws InterruptedException, ExecutionException, TimeoutException {
List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(transports));
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
StompSession stompSession = stompClient.connect("ws://localhost:" + port + "/app", new StompSessionHandlerAdapter() {}).get(1, SECONDS);
return stompSession;
}
Run Code Online (Sandbox Code Playgroud)
但是,这样我在日志中得到了异常,因为 Spring 重定向到登录页面,这通常是好的和需要的,因为 Websocket 请求未经身份验证。
日志确认:
2019-11-12 18:03:32.487 DEBUG 19592 --- [o-auto-1-exec-3] o.s.s.w.u.m.AndRequestMatcher : All requestMatchers returned true
2019-11-12 18:03:32.487 DEBUG 19592 …Run Code Online (Sandbox Code Playgroud) 在我的简化案例中,我想向所有其他客户端广播 WebSocket 客户端发送的消息。该应用程序是使用响应式 websockets 和 Spring 构建的。
我的想法是使用 single
Sink并且如果从客户端收到消息,则在此接收器上发出它。WebsocketSession::send只是将由此发出的事件转发Sink给连接的客户端。
@Component
class ReactiveWebSocketHandler(private val sink: Sinks.Many<Message>,
private val objectMapper : ObjectMapper) : WebSocketHandler {
override fun handle(session: WebSocketSession): Mono<Void> {
val input = session.receive()
.doOnNext {
sink.emitNext(fromJson(it.payloadAsText, Message::class.java), Sinks.EmitFailureHandler.FAIL_FAST)
}
.then()
val output = session.send(sink.asFlux().map { message -> session.textMessage(toJson(message)) })
return Mono.zip(input, output).then()
}
fun toJson(obj : Any) : String = objectMapper.writeValueAsString(obj)
fun <T> fromJson(json : String, clazz : Class<T>) : T{
return objectMapper.readValue(json, clazz) …Run Code Online (Sandbox Code Playgroud) spring-websocket ×10
spring ×7
java ×5
websocket ×5
stomp ×3
spring-boot ×2
kotlin ×1
redis ×1
sockjs ×1
tomcat7 ×1
tyrus ×1
unit-testing ×1