|
@@ -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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|