【问题标题】:Push Messages to Websocket Outbount using Spring Integration使用 Spring Integration 将消息推送到 Websocket Outbount
【发布时间】:2016-06-09 19:03:47
【问题描述】:

我一直在尝试使用 spring 集成和 web socket。不得不说,web sockets对我来说真的很陌生,可能我还没有了解一些基础知识。

我想做什么。我的后端正在生成消息并将它们放入 Spring-Integration 系统。这一切都很好。我的想法是,我不确定这是否有效。我有一个至少消​​息到达的频道,一个发布订阅者频道。现在我想使用 int-websocket.outbound-channel-adapter 将该消息推送到浏览器。

我的发布订阅频道名为ticketOutgoingChannel

我的配置如下:

<bean id="webSocketSessionStore" class="org.springframework.integration.metadata.SimpleMetadataStore"/>

<int-websocket:server-container id="serverWebSocketContainer" path="sass">
    <int-websocket:sockjs />
</int-websocket:server-container>

<int:chain input-channel="ticketOutgoingChannel">
    <int:header-enricher>
        <int:header 
            name="#{T(org.springframework.messaging.simp.SimpMessageHeaderAccessor).SESSION_ID_HEADER}"
            expression="webSocketSessionStore.sessionId"
        />
    </int:header-enricher>
    <int-websocket:outbound-channel-adapter container="serverWebSocketContainer" />
</int:chain>

我现在的一个问题是,我不知道如何获取 Websocket 的 sessionId,如果有,如何将其添加到消息中。

这是消息到达的通道,我想推送给客户端。

<int:publish-subscribe-channel 
    id="ticketOutgoingChannel" 
    datatype="...model.Ticket"
/>

请在 xml 中提供提示。我更喜欢 STS 图形支持的 xml cos 以便更好地理解。稍后我确定我会使用 Java-Config。

提前谢谢你。

【问题讨论】:

    标签: websocket push-notification spring-integration spring-websocket


    【解决方案1】:

    请找到WebSocket Sample 以考虑您的用例。

    在推送到浏览器之前,它必须连接。我在示例中使用简单的 JavaScript 执行此操作:

    <script type="text/javascript">
        var sock = new SockJS('http://localhost:8080/time');
        sock.onopen = function () {
            document.getElementById('time').innerHTML = 'Connecting...';
        };
        sock.onmessage = function (e) {
            document.getElementById('time').innerHTML = e.data;
        };
        sock.onclose = function () {
            document.getElementById('time').innerHTML = "Server closed connection or hasn't been started";
        };
    </script>
    

    当会话连接到服务器时,您可以从 serverWebSocketContainer.getSessions().

    然后您可以迭代 (&lt;splitter&gt;) 并应用您的 &lt;int:header-enricher&gt;

    【讨论】:

    • 您好 Artem,感谢您的回答。我已经检查了那个例子,我想它可能会有所帮助。当我有时间时,我会仔细检查并回复。感谢患者:-)
    猜你喜欢
    • 2014-08-15
    • 2019-06-08
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多