Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 websocket without STOMP,socketjs

I am trying to test websocket without using socketjs library and also i don't want to add any stomp connection.

I am following the example from stackoverflow question: WebSocket with Sockjs & Spring 4 but without Stomp

So without stomp server , I have succeeded to connect via socketjs library with a url : ws://localhost:8080/greeting/741/0tb5jpyi/websocket

And now I want to remove the socketjs library to allow raw websocket connection(may be devices such as android,ios, etc...)

When I remove the parameter : .withSockJS(), I couldn't connect via websocket.

I tried the following URLs, but they didn't work:

ws://localhost:8080/greeting/394/0d7xi9e1/websocket not worked
ws://localhost:8080/greeting/websocket not worked
ws://localhost:8080/greeting/ not worked 

which URL should i use to connect ?

like image 275
melihcoskun Avatar asked Aug 28 '15 08:08

melihcoskun


People also ask

Does spring boot support WebSocket?

The Spring Framework provides support for using STOMP — a simple, messaging protocol originally created for use in scripting languages with frames inspired by HTTP. STOMP is widely supported and well suited for use over WebSocket and over the web.

What is enableStompBrokerRelay?

The enableStompBrokerRelay method returns a convenient Registration instance that exposes a fluent API. You can use this fluent API to configure your Broker relay: registry.

What is STOMP WebSocket?

STOMP, an acronym for Simple Text Oriented Messaging Protocol, is a simple HTTP-like protocol for interacting with any STOMP message broker. Any STOMP client can interact with the message broker and be interoperable among languages and platforms.


2 Answers

I'm using websockets without STOMP in my project.

The following configuration works with spring-boot.

add spring boot websocket dependency in pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
    <version>${spring-boot.version}</version>
</dependency>

Then add a class (here WebSocketServerConfiguration.java), which configures your websocket:

@Configuration
@EnableWebSocket
public class WebSocketServerConfiguration implements WebSocketConfigurer {

    @Autowired
    protected MyWebSocketHandler webSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/as");
    }
}

finally you can write your WebsocketHandler. Spring provides you different abstract classes for WebSocketHandlers (in main-package: org.springframework.web.socket.handler). My websocket is configured without STOMP and my client doesn't use socket.js. Therefore MyWebSocketHandler extends TextWebSocketHandler and overrides the methods for errors, opening and closing connections and received texts.

@Component
public class MyWebSocketHandler extends TextWebSocketHandler {
    ...

    @Override
    public void handleTransportError(WebSocketSession session, Throwable throwable) throws Exception {
        LOG.error("error occured at sender " + session, throwable);
        ...
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        LOG.info(String.format("Session %s closed because of %s", session.getId(), status.getReason()));

        ...
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        LOG.info("Connected ... " + session.getId());

        ...
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage jsonTextMessage) throws Exception {
        LOG.debug("message received: " + jsonTextMessage.getPayload());
        ...
    }
}
like image 119
duffy356 Avatar answered Sep 19 '22 10:09

duffy356


You should use ws://localhost:8080/greeting:

new WebSocket('ws://localhost:8080/greeting')
like image 25
Sergi Almar Avatar answered Sep 22 '22 10:09

Sergi Almar