Skip to content

Commit

Permalink
web-socket模块
Browse files Browse the repository at this point in the history
  • Loading branch information
guolanren committed Jun 27, 2020
1 parent 911a651 commit 9e09eeb
Show file tree
Hide file tree
Showing 22 changed files with 430 additions and 11 deletions.
5 changes: 0 additions & 5 deletions web-socket/README.md

This file was deleted.

5 changes: 5 additions & 0 deletions web-socket/web-socket-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# web socket handler

## 概述

web socket handler 概述...
4 changes: 1 addition & 3 deletions web-socket/pom.xml → web-socket/web-socket-handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
</parent>

<groupId>name.guolanren</groupId>
<artifactId>web-socket</artifactId>
<artifactId>web-socket-handler</artifactId>
<version>1.0.0</version>
<name>web-socket</name>
<description>web socket</description>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package name.guolanren;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author guolanren
*/
@SpringBootApplication
public class WebSocketHandlerApplication {

public static void main(String[] args) {
SpringApplication.run(WebSocketHandlerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package name.guolanren.config;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Map;

/**
* @author guolanren
*/
public class MyHttpSessionHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
return super.beforeHandshake(request, response, wsHandler, attributes);
}

@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
super.afterHandshake(request, response, wsHandler, ex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package name.guolanren.config;

import name.guolanren.controller.ChattingController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
* @author guolanren
*/
@Configuration
@EnableWebSocket
public class WebSocketHandlerConfiguration implements WebSocketConfigurer {

@Autowired
private ChattingController webSocketHandler;

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
webSocketHandlerRegistry.addHandler(webSocketHandler, "/web-socket")
.addInterceptors(handshakeInterceptor())
.setAllowedOrigins("*");

// webSocketHandlerRegistry.addHandler(webSocketHandler(), "/web-socket")
// .addInterceptors(handshakeInterceptor())
// .setAllowedOrigins("*")
// .withSockJS();
}

@Bean
public HttpSessionHandshakeInterceptor handshakeInterceptor() {
return new MyHttpSessionHandshakeInterceptor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package name.guolanren.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author guolanren
*/
@RestController
public class ChattingController extends TextWebSocketHandler {

private List<WebSocketSession> sessions = new ArrayList<>();

@PostMapping("/chat")
public String chatting(@RequestBody String message) throws IOException {
for (WebSocketSession session : sessions) {
session.sendMessage(new TextMessage(message.toUpperCase()));
}
return null;
}

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
super.afterConnectionEstablished(session);
sessions.add(session);
}

@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
String receiveMsg = (String) message.getPayload();
String sendMsg = receiveMsg.toUpperCase();
session.sendMessage(new TextMessage(sendMsg));
}

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super.handleTextMessage(session, message);
}

@Override
protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
super.handlePongMessage(session, message);
}

@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
super.handleTransportError(session, exception);
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session);
super.afterConnectionClosed(session, status);
}

@Override
public boolean supportsPartialMessages() {
return super.supportsPartialMessages();
}
}
4 changes: 4 additions & 0 deletions web-socket/web-socket-javax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# web socket javax
## 概述

web socket javax 概述...
68 changes: 68 additions & 0 deletions web-socket/web-socket-javax/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>name.guolanren</groupId>
<artifactId>web-socket-javax</artifactId>
<version>1.0.0</version>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>sockjs-client</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>stomp-websocket</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* @author guolanren
*/
@SpringBootApplication
public class WebSocketApplication {
public class WebSocketJavaxApplication {

public static void main(String[] args) {
SpringApplication.run(WebSocketApplication.class, args);
SpringApplication.run(WebSocketJavaxApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package name.guolanren.config;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

/**
* @author guolanren
*/
public class HttpRequestListener implements ServletRequestListener {

@Override
public void requestInitialized(ServletRequestEvent sre) {
((HttpServletRequest) sre.getServletRequest()).getSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package name.guolanren.config;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
* @author guolanren
*/
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

@Bean
public ServletListenerRegistrationBean<HttpRequestListener> httpRequestListener() {
ServletListenerRegistrationBean<HttpRequestListener> httpRequestListener = new ServletListenerRegistrationBean<>();
httpRequestListener.setListener(new HttpRequestListener());
return httpRequestListener;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package name.guolanren.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
* @author guolanren
*/
@Configuration
public class WebSocketJavaxConfiguration {

@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}

public static class HttpSessionConfigurator extends ServerEndpointConfig.Configurator {

@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
super.modifyHandshake(sec, request, response);
HttpSession httpSession = (HttpSession) request.getHttpSession();
sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package name.guolanren.endpoint;

import name.guolanren.config.WebSocketJavaxConfiguration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author guolanren
*/
@ServerEndpoint(value = "/chat", configurator = WebSocketJavaxConfiguration.HttpSessionConfigurator.class)
@Component
public class ChattingEndpoint {

private List<WebSocketSession> sessions = new ArrayList<>();

@OnOpen
public void onOpen(Session session, EndpointConfig config) throws IOException {
System.out.println("open...");

// HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
// if (httpSession == null) {
// session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "找不到 HTTP 会话"));
// }

}

@OnClose
public void onClose() {
System.out.println("close...");
}

@OnMessage
public void onMessage(String message, Session session) throws IOException {
session.getBasicRemote().sendText(message);
}

@OnError
public void onError(Session session, Throwable error) {
error.printStackTrace();
}

}
5 changes: 5 additions & 0 deletions web-socket/web-socket-message-broker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# web socket message broker

## 概述

web socket message broker 概述...
Loading

0 comments on commit 9e09eeb

Please sign in to comment.