Move 'websocket' to 'websocket-tomcat'

This commit is contained in:
Moritz Halbritter
2022-11-25 13:24:27 +01:00
parent 620501fd1c
commit 617f075958
9 changed files with 9 additions and 9 deletions

View File

@@ -0,0 +1,48 @@
package com.example.websocket;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
class CLR implements CommandLineRunner {
private final WebSocketClient webSocketClient;
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
CLR(WebSocketClient webSocketClient, ServletWebServerApplicationContext servletWebServerApplicationContext) {
this.webSocketClient = webSocketClient;
this.servletWebServerApplicationContext = servletWebServerApplicationContext;
}
@Override
public void run(String... args) throws Exception {
WebSocketSession session = this.webSocketClient.doHandshake(new MyWebsocketHandler(), null,
URI.create("ws://localhost:%d/echo".formatted(getServerPort()))).get(5, TimeUnit.SECONDS);
TextMessage message = new TextMessage("Hello Websocket");
session.sendMessage(message);
System.out.printf("Client: Sent '%s'%n", message.getPayload());
}
private int getServerPort() {
return this.servletWebServerApplicationContext.getWebServer().getPort();
}
private static class MyWebsocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
System.out.printf("Client: Received '%s'%n", message.getPayload());
}
}
}

View File

@@ -0,0 +1,33 @@
package com.example.websocket;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Component
public class EchoWebSocketHandler extends TextWebSocketHandler {
public EchoWebSocketHandler() {
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
System.out.printf("Opened new session in instance %s%n", this);
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
System.out.printf("Server: Received '%s' in %s%n", message.getPayload(), this);
session.sendMessage(message);
System.out.printf("Server: Sent '%s' in %s%n", message.getPayload(), this);
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
System.out.printf("Closing connection in %s%n", this, exception);
session.close(CloseStatus.SERVER_ERROR);
}
}

View File

@@ -0,0 +1,13 @@
package com.example.websocket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebSocketApplication {
public static void main(String[] args) {
SpringApplication.run(WebSocketApplication.class, args);
}
}

View File

@@ -0,0 +1,31 @@
package com.example.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
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 {
private final EchoWebSocketHandler echoWebSocketHandler;
public WebSocketConfig(EchoWebSocketHandler echoWebSocketHandler) {
this.echoWebSocketHandler = echoWebSocketHandler;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(this.echoWebSocketHandler, "/echo");
}
@Bean
public WebSocketClient webSocketClient() {
return new StandardWebSocketClient();
}
}