shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.iplatform.tcp.client.config;
 
import com.iplatform.tcp.client.LoginRequest;
import com.walker.infrastructure.utils.JsonUtils;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
 
@Configuration
public class WebSocketClientConfig {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private WebSocketClient webSocketClient = null;
 
    @Bean
    public ClientProperties clientProperties(){
        return new ClientProperties();
    }
 
    @Bean
    public WebSocketClient webSocketClient(ClientProperties properties) {
//        WebSocketClient webSocketClient = null;
        try {
            webSocketClient = new WebSocketClient(new URI(properties.getUrl()),new Draft_6455()) {
                @Override
                public void onOpen(ServerHandshake handshake) {
                    logger.info("ws 连接成功");
                    // 连接上就发送登录
//                    LoginRequest request = new LoginRequest();
//                    request.setName(properties.getName());
                    String data = null;
//                    try {
//                        data = JsonUtils.objectToJsonString(request);
//                    } catch (Exception e) {
//                        throw new RuntimeException(e);
//                    }
 
                    Map<String, String> map = new HashMap<>(4);
                    map.put("protocol", "login");
                    map.put("uid", properties.getName());
                    try {
                        data = JsonUtils.objectToJsonString(map);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    webSocketClient.send(data.getBytes());
                    logger.info("客户端发送登录请求,name = {}", properties.getName());
                }
 
                @Override
                public void onMessage(String message) {
                    logger.info("ws 收到消息"+message);
                }
 
                @Override
                public void onClose(int code, String reason, boolean remote) {
                    logger.info("ws 退出");
                }
 
                @Override
                public void onError(Exception ex) {
                    ex.printStackTrace();
                    logger.info("连接错误"+ex.getMessage());
                }
            };
            webSocketClient.connect();
            return webSocketClient;
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
}