Procházet zdrojové kódy

新增webSocket客户端

anderx před 2 roky
rodič
revize
40f917a954

+ 26 - 3
src/main/java/com/goafanti/common/bean/WebSocketClientBean.java

@@ -1,21 +1,26 @@
 package com.goafanti.common.bean;
 
 import com.goafanti.common.utils.LoggerUtils;
+import com.goafanti.common.webSocket.MsgWebSocketClient;
+import com.goafanti.common.webSocket.WebClientEnum;
 import org.java_websocket.client.WebSocketClient;
 import org.java_websocket.drafts.Draft_6455;
+import org.java_websocket.enums.ReadyState;
 import org.java_websocket.handshake.ServerHandshake;
 import org.springframework.context.annotation.Bean;
 
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 
 public class WebSocketClientBean {
 
     private static String url="ws://chat.xunmengren.net:11000/ws/1683470612048";
-    @Bean
-    public WebSocketClient webSocketClient() {
+    private static String test_url="ws://localhost:8888/websocket/10086";
+//    @Bean
+    public static WebSocketClient webSocketClient() {
         try {
-            WebSocketClient webSocketClient = new WebSocketClient(new URI(url),new Draft_6455()){
+            WebSocketClient webSocketClient = new WebSocketClient(new URI(test_url),new Draft_6455()){
 
                 @Override
                 public void onOpen(ServerHandshake serverHandshake) {
@@ -36,7 +41,14 @@ public class WebSocketClientBean {
                 public void onError(Exception e) {
                     LoggerUtils.debug(getClass(),"报错了:::" + e.getMessage());
                 }
+
+                @Override
+                public void send(String text) {
+                    LoggerUtils.debug(getClass(),"发送消息:::"+text);
+                    super.send(text);
+                }
             };
+
             webSocketClient.connect();
             return webSocketClient;
         } catch (Exception e) {
@@ -44,4 +56,15 @@ public class WebSocketClientBean {
         }
         return null;
     }
+
+
+    public static void main(String[] args) {
+        WebSocketClient webSocketClient = webSocketClient();
+        //先等待连接上
+        while (!webSocketClient.getReadyState().equals(ReadyState.OPEN)){
+
+        }
+        webSocketClient.send("666");
+        webSocketClient.close();
+    }
 }

+ 45 - 0
src/main/java/com/goafanti/common/webSocket/MsgWebSocketClient.java

@@ -0,0 +1,45 @@
+package com.goafanti.common.webSocket;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Iterator;
+
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.handshake.ServerHandshake;
+
+public class MsgWebSocketClient extends WebSocketClient{
+
+    public MsgWebSocketClient(String url) throws URISyntaxException {
+        super(new URI(url));
+        // TODO Auto-generated constructor stub
+    }
+
+    @Override
+    public void onOpen(ServerHandshake shake) {
+        // TODO Auto-generated method stub
+        System.out.println("握手...");
+        for(Iterator<String> it=shake.iterateHttpFields();it.hasNext();) {
+            String key = it.next();
+            System.out.println(key+":"+shake.getFieldValue(key));
+        }
+    }
+
+    @Override
+    public void onMessage(String paramString) {
+        // TODO Auto-generated method stub
+        System.out.println("接收到消息:"+paramString);
+    }
+
+    @Override
+    public void onClose(int paramInt, String paramString, boolean paramBoolean) {
+        // TODO Auto-generated method stub
+        System.out.println("关闭...");
+    }
+
+    @Override
+    public void onError(Exception e) {
+        // TODO Auto-generated method stub
+        System.out.println("异常"+e);
+
+    }
+}

+ 47 - 0
src/main/java/com/goafanti/common/webSocket/WebClientEnum.java

@@ -0,0 +1,47 @@
+package com.goafanti.common.webSocket;
+
+
+import org.apache.commons.lang3.ObjectUtils;
+import org.java_websocket.WebSocket;
+import org.java_websocket.enums.ReadyState;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+
+
+public enum WebClientEnum {
+
+    CLIENT;
+
+    private static MsgWebSocketClient socketClient = null;
+
+    public static void initClient(MsgWebSocketClient client) {
+        socketClient = client;
+        if(socketClient!=null) {
+            socketClient.connect();
+            while (socketClient.getReadyState().equals(ReadyState.OPEN)){
+                socketClient.send("测试websocket。。。");
+            }
+
+        }
+        boolean flag = true;
+        int i=1000;
+        while(flag) {
+            while (!socketClient.getReadyState().equals(ReadyState.OPEN)){
+
+            }
+            socketClient.send("测试websocket。。。"+(i--));
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+            if(i == 0) {
+                flag = false;
+            }
+        }
+    }
+
+}