vico
vico

Reputation: 18171

Tomcat websocket closes connection

I run simple websocket server on Tomcat:

package server.ws;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;




@ServerEndpoint("/websocketendpoint")

public class WsServer {

    @OnOpen
    public void onOpen(){
        System.out.println("Open Connection ...");
    }

    @OnClose
    public void onClose(){
        System.out.println("Close Connection ...");
    }

    @OnMessage
    public String onMessage(String message){
        System.out.println("Message from the client: " + message);
        String echoMsg = "Echo from the server : " + message;
        return echoMsg;
    }

    @OnError
    public void onError(Throwable e){
        e.printStackTrace();
    }

}

Client that connects to my server complains regarding Tomcat closes session after echo was transmitted. How to make the server to not close the connection after echo?

Upvotes: 1

Views: 855

Answers (1)

user4602302
user4602302

Reputation:

i'm not sure, but i guess onMessagehas no return value. for me, the following code works fine and i have any problems

@OnMessage
public void message(final String message, final javax.websocket.Session session) throws IOException {
    for (final Session s : session.getOpenSessions()) {
        final Basic endpoint = s.getBasicRemote();
        endpoint.sendText(message);
    }
}

Upvotes: 1

Related Questions