关闭所有使用Spring GraphQL的WebSocket会话

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

Closing all WebSocket sessions using Spring GraphQL

问题

我正在开发一个Spring GraphQL项目(使用最新的spring-boot-starter-graphql),并且正在使用WebSocket建立订阅。然而,我在编程方式关闭所有WebSocket会话方面遇到了问题。

我已经查阅了文档并搜索了示例,但我无法找到针对Spring GraphQL的明确解决方案。我找到的大多数资源与一般的WebSocket实现或STOMP有关。

框架有一个处理程序(GraphQlWebSocketHandler#WebMvcSessionInfo),它包装了WebSocketSession并将其放在内部的sessionInfoMap中,不会在外部共享。

我希望在特定事件发生时,能够确保所有活动会话都被正确终止。对于任何帮助或演示该方法的代码示例,我将不胜感激。谢谢!

英文:

I'm working on a Spring GraphQL (newest spring-boot-starter-graphql) project and I'm using WebSocket to establish subscriptions. However, I'm having trouble finding a way to close all WebSocket sessions programmatically.

I've looked into the documentation and searched for examples, but I couldn't find a clear solution specific to Spring GraphQL. Most of the resources I found were related to general WebSocket implementations or STOMP.

Framework has a handler (GraphQlWebSocketHandler#WebMvcSessionInfo) that wraps WebSocketSession and places it in an internal sessionInfoMap that is not shared externally.

I want to make sure that all active sessions are properly terminated when a specific event occurs.
Any help or code examples demonstrating the approach would be greatly appreciated. Thank you!

答案1

得分: 1

我通过扩展GraphQlWebSocketHandler并在bean集合中收集WebSocket会话来找到解决方案。

@Component
class CustomGraphqlWebSocketHandler extends GraphQlWebSocketHandler {

    private final Collection<WebSocketSession> sessions;

    CustomGraphqlWebSocketHandler(WebGraphQlHandler graphQlHandler, HttpMessageConverter<?> mappingJackson2HttpMessageConverter,
                                  Collection<WebSocketSession> sessions) {
        super(graphQlHandler, mappingJackson2HttpMessageConverter, Duration.ofSeconds(5));
        this.sessions = sessions;
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        sessions.add(session);
        super.afterConnectionEstablished(session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
        sessions.remove(session);
        super.afterConnectionClosed(session, closeStatus);
    }
}

请注意,代码部分已保留在英文状态。

英文:

I found the solution by extending GraphQlWebSocketHandler and collecting web socket sessions in bean collection.

@Component
class CustomGraphqlWebSocketHandler extends GraphQlWebSocketHandler {

    private final Collection&lt;WebSocketSession&gt; sessions;

    CustomGraphqlWebSocketHandler(WebGraphQlHandler graphQlHandler, HttpMessageConverter&lt;?&gt; mappingJackson2HttpMessageConverter,
                                  Collection&lt;WebSocketSession&gt; sessions) {
        super(graphQlHandler, mappingJackson2HttpMessageConverter, Duration.ofSeconds(5));
        this.sessions = sessions;
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        sessions.add(session);
        super.afterConnectionEstablished(session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
        sessions.remove(session);
        super.afterConnectionClosed(session, closeStatus);
    }
}

huangapple
  • 本文由 发表于 2023年5月29日 16:44:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355850.html
匿名

发表评论

匿名网友

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

确定