gpt4 book ai didi

java - 如何在 Spring Boot 中将区域设置从 http 请求传递到 websocket?

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:49 26 4
gpt4 key购买 nike

如何在 Spring Boot 中将区域设置从 http 请求传递到 websocket?

我的区域设置已经在 LocaleContextHolder 中设置,但是当我移交给 websocket 时,它消失了并且再次默认。将区域设置移交给 websockets 的正确方法是什么?

最佳答案

好的,我找到了解决方案。由于 LocaleContextHolder 是基于线程的,并且 websocket 是异步运行的,因此请求中的内容会丢失。但幸运的是,有 HandshakeInterceptor 将某些事情交给 websocket session 。

我的配置:

@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketBrokerConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> {

// ...

@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOrigins("*")
.addInterceptors(new HttpWebsocketHandshakeInterceptor()) // <-- The interceptor
.withSockJS();
}

// ...

}

拦截器:

public class HttpWebsocketHandshakeInterceptor implements HandshakeInterceptor {

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {

if (request instanceof ServletServerHttpRequest) {
Locale locale = LocaleContextHolder.getLocale();
attributes.put(WSConstants.HEADER_HTTP_LOCALE, locale);

// hand over more stuff, if needed ...
}
return true;
}

@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception exception) {

}

}

WSConstants.HEADER_HTTP_LOCALE 只是一个字符串常量。随便你怎么调用它。

然后在你的 Controller 中:

@Controller
public class WSController {

@MessageMapping("/somewhere/")
public void message(
SimpMessageHeaderAccessor headerAccessor,
Principal principal,
WSMessage frame) {

// update locale to keep it up to date
Map<String, Object> sessionHeaders = headerAccessor.getSessionAttributes();
Locale locale = (Locale) sessionHeaders.get(WSConstants.HEADER_HTTP_LOCALE);
if (locale != null) {
LocaleContextHolder.setLocale(locale);
}

// use your localized stuff as you used to

}

@SubscribeMapping("/somewhereelse/")
public ChannelPayload bubble(
SimpMessageHeaderAccessor headerAccessor,
Principal principal
) {

// update locale to keep it up to date
Map<String, Object> sessionHeaders = headerAccessor.getSessionAttributes();
Locale locale = (Locale) sessionHeaders.get(WSConstants.HEADER_HTTP_LOCALE);
if (locale != null) {
LocaleContextHolder.setLocale(locale);
}

// use your localized stuff as you used to

return null;
}

}

希望这可以帮助其他遇到同样问题的人。

关于java - 如何在 Spring Boot 中将区域设置从 http 请求传递到 websocket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56878771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com