소스 검색

新增webSocket客户端

anderx 2 년 전
부모
커밋
9f5d233e01

+ 6 - 0
pom.xml

@@ -90,6 +90,12 @@
 			<artifactId>spring-websocket</artifactId>
 			<version>${org.springframework-version}</version>
 		</dependency>
+		<!--webSocket客户端-->
+		<dependency>
+			<groupId>org.java-websocket</groupId>
+			<artifactId>Java-WebSocket</artifactId>
+			<version>1.4.0</version>
+		</dependency>
 		<dependency>
 			<groupId>org.springframework</groupId>
 			<artifactId>spring-messaging</artifactId>

+ 47 - 0
src/main/java/com/goafanti/common/bean/WebSocketClientBean.java

@@ -0,0 +1,47 @@
+package com.goafanti.common.bean;
+
+import com.goafanti.common.utils.LoggerUtils;
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.drafts.Draft_6455;
+import org.java_websocket.handshake.ServerHandshake;
+import org.springframework.context.annotation.Bean;
+
+import java.net.URI;
+import java.net.URL;
+
+public class WebSocketClientBean {
+
+    private static String url="ws://chat.xunmengren.net:11000/ws/1683470612048";
+    @Bean
+    public WebSocketClient webSocketClient() {
+        try {
+            WebSocketClient webSocketClient = new WebSocketClient(new URI(url),new Draft_6455()){
+
+                @Override
+                public void onOpen(ServerHandshake serverHandshake) {
+                    LoggerUtils.debug(getClass(),"建立连接");
+                }
+
+                @Override
+                public void onMessage(String s) {
+                    LoggerUtils.debug(getClass(),"收到来自服务端消息:"+s);
+                }
+
+                @Override
+                public void onClose(int i, String s, boolean b) {
+                    LoggerUtils.debug(getClass(),"关闭连接:::" + "i = " + i + ":::s = " + s +":::b = " + b);
+                }
+
+                @Override
+                public void onError(Exception e) {
+                    LoggerUtils.debug(getClass(),"报错了:::" + e.getMessage());
+                }
+            };
+            webSocketClient.connect();
+            return webSocketClient;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+}

+ 11 - 1
src/main/java/com/goafanti/common/controller/PublicController.java

@@ -912,7 +912,17 @@ public class PublicController extends BaseController {
 		return res.data(str);
 	}
 
-
+	@RequestMapping(value = "/SelectwebSocket",method = RequestMethod.GET)
+	@ResponseBody
+	public Result  SelectwebSocket (String text){
+		Result res=new Result();
+		Map<String,Object> params=new HashMap();
+		List<String> list =new ArrayList<>();
+		params.put("type","text");
+		params.put("msg",text);
+//		MyWebSocketClient
+		return res.data("");
+	}
 
 
 

+ 1 - 1
src/main/java/com/goafanti/core/websocket/SystemWebSocketHandler.java

@@ -84,4 +84,4 @@ public class SystemWebSocketHandler implements WebSocketHandler {
 			}
 		}
 	}
-}
+}

+ 5 - 1
src/main/java/com/goafanti/core/websocket/WebSocketHandshakeInterceptor.java

@@ -24,6 +24,10 @@ public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {
 				// 使用TokenManager.getUserId区分WebSocketHandler,以便定向发送消息
 				attributes.put(Constants.WEBSOCKET_USERNAME, TokenManager.getUserId());
 				return true;
+			}else {
+				//非登录用户
+				attributes.put(Constants.WEBSOCKET_USERNAME, 10086l);
+				return true;
 			}
 		}
 		return false;
@@ -34,4 +38,4 @@ public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {
 			Exception exception) {
 
 	}
-}
+}

+ 2 - 0
src/main/resources/shiro_base_auth.ini

@@ -16,6 +16,8 @@
 /managesignin=anon
 /register=anon
 /mregister=anon
+/webSocketServer=anon
+/sockjs/webSocketServer=anon
 /open/**=anon
 /favicon.ico=anon
 /static/**=anon