Implementing WebSocket on Spring MVC based Server

1.4k views Asked by At

I searched a lot about implementing WebSocket/XMPP on Spring MVC based server but couldn't reach to a concrete answer. So here is my requirement

  1. Receive a message from a client (in my case it will be a android/iOS mobile) via WebSocket/XMPP on tomcat server and parse the actual message at server side
  2. Send a message from server app to WebSocket/XMPP client

If somebody could help me to point on some good tutorial or demo code, it would be a great help.

3

There are 3 answers

0
James Barwick On

run Tomee 1.5.2

http://openejb.apache.org/downloads.html

activate the ActiveMQ JMS Server. create an OpenEJB configuration.

http://www.mail-archive.com/[email protected]/msg04327.html

setup an XMPP ActiveMQ server protocol listener (in the activemq.xml)

in your Spring services configuration, create a Spring JMS listener (Spring ListenerContainer) configuration on the Topic/Queue.

you can use the JmsTemplate to push a message out to the Queue/Topic via ActiveMQ, the XMPP client will receive the message.

Enjoy!

BTW: This is exactly what I am in the middle of setting up right now...still learning.

0
Jasonw On

check this out: www.xchat.io. It was built based on Asynchronous Spring MVC (DefferredResult, you know), XMPP, and jQuery. it's promising.

0
Badal On

I am not sure if this is just perfect way to achieve or not, but for now I have found a solution and it would be glad to share it here.

There are two steps that you have to done. 1. Instead of ordinary HTTPServlet sub class, create a sub class of WebSocketServlet and 2. Create a sub class of MessageInbound class and override its required methods. P.S. : Only latest version of tomcat supports WebSocket (apache tomcat 7.0.42 or higher).

Here is a WebSocket class.

public class WsChatServlet extends WebSocketServlet {

    private static final long serialVersionUID = 1456546233L;

    @Override
    protected StreamInbound createWebSocketInbound(String protocol,
            HttpServletRequest request) {
        return new IncomingMessageHandler();
    }
}

And this is a simple class which can send/receive message (String/binary).

public class IncomingMessageHandler extends MessageInbound {

    @Override
    public void onOpen(WsOutbound outbound) {
        logger.info("Open Client.");
    }

    @Override
    public void onClose(int status) {
        logger.info("Close Client.");
    }

    @Override
    public void onTextMessage(CharBuffer cb) throws IOException {
        logger.info("Text Message received:" + cb.toString());
    }

    @Override
    public void onBinaryMessage(ByteBuffer bb) throws IOException {

    }

    public synchronized void sendTextMessage(String message) {
        try {
            CharBuffer buffer = CharBuffer.wrap(message);
            this.getMyoutbound().writeTextMessage(buffer);
            this.getMyoutbound().flush();

        } catch (IOException e) {
            // Handle Exception
        }
    }
}