Move 'websocket' to 'websocket-tomcat'
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user