提问人:Alex 提问时间:8/16/2023 更新时间:8/16/2023 访问量:50
Apache 和 Wildfly 的 WebSocket 问题
WebSocket problem with Apache and Wildfly
问:
当我部署我的 Spring 应用程序时,我遇到 websocket 这个问题
Forwarding to error page from request [/active-users] due to exception [Servlet request failed to upgrade to WebSocket: https://remote-host/test/active-users; nested exception is javax.servlet.ServletException: java.lang.IllegalStateException: UT000077: The underlying transport does not support HTTP upgrade]
我目前使用的是 Apache 2.2 和 wildfly 18(我无法更新它们)
在我的 Wildfly 中,我有这样的 websocket 配置:
<subsystem xmlns="urn:jboss:domain:undertow:10.0" default-server="default-server" default-virtual-host="default-host" default-servlet-container="default" default-security-domain="other" statistics-enabled="true">
<buffer-cache name="default"/>
<server name="default-server">
<ajp-listener name="ajp" socket-binding="ajp" record-request-start-time="true" redirect-socket="http"/>
<http-listener name="default" socket-binding="http" record-request-start-time="true" redirect-socket="https" enable-http2="true"/>
<https-listener name="https" socket-binding="https" record-request-start-time="true" security-realm="ApplicationRealm" enable-http2="true"/>
<host name="default-host" alias="localhost">
<location name="/" handler="welcome-content"/>
<http-invoker security-realm="ApplicationRealm"/>
</host>
</server>
<servlet-container name="default">
<jsp-config/>
<websockets/>
</servlet-container>
<handlers>
<file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
</handlers>
</subsystem>
在我的春季应用程序中,我有这 2 个类:
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Autowired
private ActiveUsersWebSocketHandler activeUsersWebSocketHandler;
@Bean
public TaskScheduler defaultSockJsTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(activeUsersWebSocketHandler, "/active-users")
.setAllowedOrigins("*");
}
和:
@Slf4j
@Configuration
public class ActiveUsersWebSocketHandler extends TextWebSocketHandler {
private Set<WebSocketSession> activeSessions = new HashSet<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
log.info("{}.{} ", this.getClass().getName(), "afterConnectionEstablished");
activeSessions.add(session);
sendActiveUsersCount();
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
log.info("{}.{} ", this.getClass().getName(), "afterConnectionClosed");
activeSessions.remove(session);
sendActiveUsersCount();
}
private void sendActiveUsersCount() {
log.info("{}.{} sending users count", this.getClass().getName(), "sendActiveUsersCount");
Map<String, Object> payload = new HashMap<>();
payload.put("activeUsersCount", activeSessions.size());
ObjectMapper objectMapper = new ObjectMapper();
String jsonPayload;
try {
jsonPayload = objectMapper.writeValueAsString(payload);
} catch (JsonProcessingException e) {
log.error("{}.{} error: {}", this.getClass().getName(), "sendActiveUsersCount", e.getMessage());
throw new CustomException(HttpStatus.INTERNAL_SERVER_ERROR, "Error", e.getMessage());
}
for (WebSocketSession session : activeSessions) {
try {
session.sendMessage(new TextMessage(jsonPayload));
} catch (IOException e) {
log.error("{}.{} error while sending message: {}", this.getClass().getName(), "sendActiveUsersCount", e.getMessage());
throw new CustomException(HttpStatus.INTERNAL_SERVER_ERROR, "Error", e.getMessage());
}
}
}
您是否建议我解决错误而无需将 apache 更新到 2.4,这也是我的 apache 配置:
80:
# WebSocket proxy
ProxyPass /ws/ ws://localhost:8009/
ProxyPassReverse /ws/ ws://localhost:8009/test
# WebSocket Secure proxy
ProxyPass /wss/ wss://localhost:8009/nnn
ProxyPassReverse /wss/ wss://localhost:8009/test
443:
# WebSocket proxy
ProxyPass /ws/ ws://localhost:8009/
ProxyPassReverse /ws/ ws://localhost:8009/test
# WebSocket Secure proxy
ProxyPass /wss/ wss://localhost:8009/test
ProxyPassReverse /wss/ wss://localhost:8009/test
答: 暂无答案
评论