a.hrdie
a.hrdie

Reputation: 716

Connect to URL using java socket

I have a STOMP websocket server accepting connections on localhost:8080/price-stream/ws

I am attempting to connect using the Gozirra library which uses java.net.socket to connect, but the new Socket() method only accepts host and port parameters. Connection code here . Is there a way to define the full resource location in the socket parameters, to include the price-stream/ws location?

Client c;
    try {
        c = new Client("localhost", 8080, "guest", "guest");
        c.subscribe("topic/pricechannel1", new Listener() {

            @Override
            public void message(Map headers, String body) {

                System.out.println(body);
            }

        });
        c.unsubscribe("topic/pricechannel1"); // Unsubscribe all listeners
        c.disconnect();
    } catch (LoginException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Failing that, is there a way on the server-side I could forward a request for a Spring websocket connection? In other words, my server is a Spring webapp running on tomcat. Is there a way I could forward requests to the root of my server i.e. localhost:8080 in this case, into a specified resource location in tomcat. I am not very well versed in linux server configuration but I assume it is possible with some tweaking. Thanks

Upvotes: 1

Views: 2480

Answers (1)

a.hrdie
a.hrdie

Reputation: 716

I ended up using the Spring 4.2 StompClient to connect to my host here is the code:

WebSocketClient transport = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(transport);
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
stompClient.setMessageConverter(converter);
StompSessionHandler handler = new WSClient(); //custom implementation
String url = "ws://{URL}/ws/websocket";
stompClient.connect(url, handler);

Then configured my WSClient class to subscribe to the channel after a connection is established.

session.subscribe("{channel name}", new StompFrameHandler() {

Upvotes: 1

Related Questions