5

I'm currently using Struts 2 as my framework and I need to have a Websocket feature so I can communicate with my client that is accessing it through HTML Websocket.

I have tried to use Java Websocket API (JSR 356) with Java application running on Tomcat 7.0.56. However, when I try it with Struts 2 framework, it does not work.

Some researches that I did suggested that it could have been because of the way Struts 2 maps the URL, but to no avail, I am still unable to communicate with the Websocket endpoint on my server.

Do anyone have any idea how to implement Websocket with Struts 2 framework?

The code that I used for the websocket is as follow:

@ServerEndpoint("/mssendpoint")
public class MSSEndpoint {

    public static Logger logger = Logger.getLogger(MSSEndpoint.class);

    /* Queue for all open WebSocket sessions */
    static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();

    static Set<WebsocketListener> listeners = new HashSet<WebsocketListener>();

    public static void send(String msg) {
        try {
            /* Send updates to all open WebSocket sessions */
            for (Session session : queue) {
                session.getBasicRemote().sendText(msg);
                logger.info("Sent: " + msg);
            }
        }
        catch (IOException e) {
            logger.error(e.toString());
        }
    }

    @OnOpen
    public void openConnection(Session session) {
        /* Register this connection in the queue */
        queue.add(session);
        logger.info("Connection opened.");
    }

    @OnClose
    public void closedConnection(Session session) {
        /* Remove this connection from the queue */
        queue.remove(session);
        logger.info("Connection closed.");
    }

    @OnError
    public void error(Session session, Throwable t) {
        /* Remove this connection from the queue */
        queue.remove(session);
        logger.info(t.toString());
        logger.info("Connection error.");
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        if (queue.contains(session)) {
            notifyListener(message);
        }
    }

    public static void addListener(WebsocketListener listener){
        listeners.add(listener);
    }

    public static void removeListener(WebsocketListener listener){
        listeners.remove(listener);
    }

    public void notifyListener(String message){
        for (WebsocketListener listener : listeners) {
            listener.onMessage(message);
        }
    }
}

I have used this exact same code on normal Java Servlet application running on Tomcat 7.0.56 and with a client, I could connect to it.

I used 'Simple Websocket Client' chrome extension as the client.

All I need was to connect to ws://localhost/myprojectname/mssendpoint and it will connect directly.

EDIT2: I forgot to mention that the error was that when I tried to connect, it will simply say undefined when I use the Websocket Client. Assuming that my Struts 2 project is called cms, by right I should just need to access ws://localhost/myprojectname/mssendpoint. But then it produces that undefined message.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alvin Tandian
  • 171
  • 1
  • 9
  • Add your code that you have tried and the error message and/or explain how it is not working. – Aleksandr M Jan 30 '15 at 06:46
  • 1
    Did you forgot a port in your url? Something like `ws://localhost:8080/...`. And of course read this: http://stackoverflow.com/q/17293115/1700321. – Aleksandr M Jan 30 '15 at 10:22
  • possible duplicate of [Using a WebSocket API with Struts 2](http://stackoverflow.com/questions/20774716/using-a-websocket-api-with-struts-2) – Roman C Jan 30 '15 at 11:08

0 Answers0