2

I am using Glassfish server to host a basic Web Socket server using javax.websocket.

Here is the server code:

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint("/websocket/server")
public class WebSocketServer {
    @OnOpen
    public void onOpen(Session session) {
        System.out.println(String.format("Connection Established::%s", session.getId()));
        RemoteEndpoint.Basic remoteEndpoint = session.getBasicRemote();
        session.addMessageHandler(new ServerMessageHandler(remoteEndpoint));
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println(String.format("Connection closed::%s", session.getId()));
    }

    @OnError
    public void onError(Throwable t) {
        System.out.println(t.toString());
    }

    @OnMessage
    public void onMessage(Session session, String message) {
        System.out.println(String.format("Received message::%s - sessionId::%s", message, session.getId()));
    }

    private class ServerMessageHandler implements MessageHandler.Whole<String> {
        private RemoteEndpoint.Basic _remoteEndpoint;

        ServerMessageHandler(RemoteEndpoint.Basic remoteEndpoint) {
            this._remoteEndpoint = remoteEndpoint;
        }
        public void onMessage(String message) {
            try {
                _remoteEndpoint.sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

And here is the console app that runs the server:

    import org.glassfish.tyrus.server.Server;

import javax.websocket.DeploymentException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        runServer();
    }

    private static void runServer() {
        Server server = null;
        try {
            server = new Server("localhost", 8080, "/websocket", null, WebSocketServer.class);
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            server.start();
            System.out.println("Please press a key to stop the server.");
            reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (DeploymentException e) {
            throw new RuntimeException(e);
        } finally {
            if (server != null) {
                server.stop();
            }
        }
    }
}

My question is when I run this console app and it runs the Websocket server, what is the best way I can test my endpoint to verify that it's working or even debug into the server code (onOpen, onClose, etc). I was attempting to use Postman to hit ws:\localhost:8080\websocket\server, but that doesn't work. I am completely new to Web Sockets, so my thinking could be wrong due to lack of experience and knowledge with the technology.

Thanks.

Matt
  • 99
  • 1
  • 2
  • 7

2 Answers2

3

Postman doesn't support it, but WebSocket King provides a similar interface that is designed specifically for web sockets.

Tom
  • 950
  • 3
  • 15
  • 27
1

https://postwoman.io/ also supports websocket connections, however it's kinda inconvinient to use as it even bans copy from response

personally, I use https://www.websocket.org/echo.html page with custom URL, but if you want code snippets, pre and post-request testing, I would recommend to write your own client

Alveona
  • 850
  • 8
  • 17