Spring A Simple WebSockets Download Example

I am trying to create a very simple socket server example in Spring Boot. I have the following classes

WebSocketConfigurer:

@Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "/path"); } @Bean public WebSocketHandler myHandler() { return new SimpleWebSocketHandler(); } } 

TextWebSocketHandler:

 public class SimpleWebSocketHandler extends TextWebSocketHandler { private static Logger logger = LoggerFactory.getLogger(SimpleWebSocketHandler.class); @Override public void afterConnectionEstablished(WebSocketSession session) { logger.debug("Opened new session in instance " + this); } @Override public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { String echoMessage = message.getPayload(); logger.debug(echoMessage); } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { session.close(CloseStatus.SERVER_ERROR); } } 

Application:

 @SpringBootApplication public class WebSocketServerApplication { public static void main(String[] args) { SpringApplication.run(WebSocketServerApplication.class, args); } } 

Then I have a C program that will open the socket and send plain text to the server.

When I try to send only plain text (for example, "Hello") to the socket, I get the following error message to the client:

 HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Transfer-Encoding: chunked Date: Tue, 18 Aug 2015 09:22:05 GMT Connection: close 

And the server displays the message "Request header of the HTTP error message." I assume this is to be expected since the server runs on the tomcat embedded server and is configured for HTTP only.

When I then try to send an HTTP POST message as text from application C, for example.

 POST /path HTTP/1.0\r\n Content-Type: text/plain\r\n Content-Length: 5\r\n \r\n Hello 

I do not get anything on the server side, and the connection to the client eventually just disconnects.

Am I missing something?

Do I need to specify a controller class? Doesn't that apply when I register a web socket handler along the way?

Thanks for the help...

+4
source share

All Articles