2

我正在试用Spring集成和Web套接字的实验。我不得不说,网络套接字对我来说真的很新鲜,也许我还没有明白一些基础知识。使用Spring集成将消息推送到Websocket Outbount

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

我的发布 - 订阅通道被命名为​​

我的配置看起来像如下:

<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> 

我的一个问题吧,我不`吨知道如何获得的sessionId对WebSocket的,如果我有它,如何将它添加到消息。

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

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

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

预先感谢您。

回答

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()获得所有他们的服务器。

然后您可以对它们进行迭代(<splitter>)并应用您的<int:header-enricher>

+0

嗨阿尔乔姆,谢谢你的回答。我已经检查过这个例子,我想这可能会有所帮助。当我再次有时间时,我会接受更仔细的检查,然后回复。感谢患者:-) –

相关问题