-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
430 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# web socket handler | ||
|
||
## 概述 | ||
|
||
web socket handler 概述... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
web-socket/web-socket-handler/src/main/java/name/guolanren/WebSocketHandlerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...socket-handler/src/main/java/name/guolanren/config/MyHttpSessionHandshakeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...web-socket-handler/src/main/java/name/guolanren/config/WebSocketHandlerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...socket/web-socket-handler/src/main/java/name/guolanren/controller/ChattingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# web socket javax | ||
## 概述 | ||
|
||
web socket javax 概述... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
web-socket/web-socket-javax/src/main/java/name/guolanren/config/HttpRequestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ket/web-socket-javax/src/main/java/name/guolanren/config/WebSocketJavaxConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
web-socket/web-socket-javax/src/main/java/name/guolanren/endpoint/ChattingEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# web socket message broker | ||
|
||
## 概述 | ||
|
||
web socket message broker 概述... |
Oops, something went wrong.