i want to configure a simple websocket for notification.. in springboot i created two files webSocketConfig and webSocketController
package com.example.demo.websocket;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new WebSocketController(), "/ws")
.setAllowedOrigins("*");// i t
}
}
package com.example.demo.websocket;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class WebSocketController extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
// Echo back the message to the client
try {
session.sendMessage(new TextMessage("Echo: " + message.getPayload()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
port : Tomcat started on port(s): 8088 (http) with context path ''
when i tried to test it in hopposcotch i write ws://localhost:8088/ws and i got error in connection(something was wrong and then disconnected)..any help plz