I'd have to know more about the protocol to advise the best approach but, typically, if the server can send unsolicited messages at any time, then the gateway is not appropriate (it is for purely request/response scenarios); you would need to use a pair of collaborating channel adapters. You just have to keep a handle to the connection id (from the message header) to send the unsolicited messages.
If the "extra" responses are only during handshake, you should be able to achieve that with a custom (de)serializer and possibly a connection interceptor, and the gateway should work ok.
The WebSocket extension (https://github.com/SpringSource/spri...-ip-extensions) uses the collaborating adapters approach for full asynchronous communication after handshaking is complete. It uses a combination of custom (de)serializers, an interceptor, and a custom TCP message mapper (to add additional headers without having to copy the message in the interceptor). The interceptor itself also sends out unsolicited messages (PINGs) when there has been no activity on the socket. The project also introduces the concept of a stateful deserializer (WebSockets switches from initially using HTTP) and the interceptor uses that state when decoding messages. Finally, this extension uses the new TCP eventing mechanism to alert the application when a new handshake is complete - allowing the server to send outbound messages before it receives any requests.
The test case (WebSocketServerTests) sends unsolicited messages to the browser (ws.html) after the hanshake is complete.
It doesn't have much in the way of java docs right now so let me know if you have any questions.
Depending on how complicated your protocol is, you may not need the full sophistication used there but, hopefully, it points you in the right direction.
Hope this helps.
If you think the solution will have wide appeal, feel free to submit it to the extensions repository (perhaps just a pull-request to the spring-integration-ip-extensions project, where the WebSocket extension resides).
See https://github.com/SpringSource/spri...ONTRIBUTING.md
Note: this discussion is from the server's perspective; if you are the client, similar concepts apply.
Last edited by Gary Russell; Mar 27th, 2013 at 09:15 AM.
Gary P. Russell
Spring Integration Team
SpringSource, a division of VMware