private static void ReConnect() { if (Socket != null) { Socket.Close(); } try { Socket = new Socket(SocketType.Stream, ProtocolType.Tcp); Socket.Connect(IPAddress.Parse("127.0.0.1"), 23333); var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(PackStart) + " "); data[data.Length - 1] = 0; Socket.Send(data); QueueRead.Clear(); QueueSend.Clear(); ServerMain.LogOut("机器人已连接"); IsConnect = true; } catch (Exception e) { ServerMain.LogError("机器人连接失败"); ServerMain.LogError(e); } }
private static void ReConnect() { if (Socket != null) { Socket.Close(); } try { Socket = new Socket(SocketType.Stream, ProtocolType.Tcp); Socket.Connect(IPAddress.Parse("127.0.0.1"), 23333); var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(PackStart) + " "); data[data.Length - 1] = 0; Socket.Send(data); while (Socket.Available == 0) { Thread.Sleep(10); } data = new byte[Socket.Available]; Socket.Receive(data); QQs = JArray.Parse(Encoding.UTF8.GetString(data)).ToObject <List <long> >(); QueueRead.Clear(); QueueSend.Clear(); ServerMain.LogOut("机器人已连接"); IsConnect = true; } catch (Exception e) { ServerMain.LogError("机器人连接失败"); ServerMain.LogError(e); } }
public static void Start() { QueueRead = new ConcurrentBag <RobotTask>(); QueueSend = new ConcurrentBag <byte[]>(); DoThread = new Thread(() => { RobotTask task; while (IsRun) { try { if (QueueRead.TryTake(out task)) { switch (task.index) { case 28: var pack5 = JsonConvert.DeserializeObject <GroupMessagePostSendEventPack>(task.data); if (pack5.res && pack5.message[pack5.message.Count - 1] == "3秒后撤回") { Task.Run(() => { Thread.Sleep(2900); string id = Utils.GetString(pack5.message[0], "source:", ","); var data = BuildPack.Build(new ReCallMessage { id = long.Parse(id) }, 71); QueueSend.Add(data); }); } break; case 46: var pack3 = JsonConvert.DeserializeObject <NewFriendRequestEventPack>(task.data); Console.WriteLine("id = " + pack3.id); Console.WriteLine("fid = " + pack3.fid); Console.WriteLine("name = " + pack3.name); Console.WriteLine("message = " + pack3.message); Console.WriteLine("eventid = " + pack3.eventid); Console.WriteLine(); CallEvent(pack3.eventid, 0, null); break; case 49: var pack = JsonConvert.DeserializeObject <GroupMessageEventPack>(task.data); Console.WriteLine("id = " + pack.id); Console.WriteLine("fid = " + pack.fid); Console.WriteLine("name = " + pack.name); Console.WriteLine("message = "); foreach (var item in pack.message) { Console.WriteLine(item); } Console.WriteLine(); if (pack.message[pack.message.Count - 1] == "撤回") { string id = Utils.GetString(pack.message[0], "source:", ","); var data = BuildPack.Build(new ReCallMessage { id = long.Parse(id) }, 71); QueueSend.Add(data); } else if (pack.message[pack.message.Count - 1] == "回复") { string id = Utils.GetString(pack.message[0], "source:", ","); var list2 = new List <string>() { "quote:" + id }; list2.Add("回复消息"); SendGroupMessage(pack.id, list2); } else if (pack.message[pack.message.Count - 1] == "撤回自己") { var list2 = new List <string>() { "3秒后撤回" }; SendGroupMessage(pack.id, list2); } break; case 50: var pack1 = JsonConvert.DeserializeObject <TempMessageEventPack>(task.data); Console.WriteLine("id = " + pack1.id); Console.WriteLine("fid = " + pack1.fid); Console.WriteLine("name = " + pack1.name); Console.WriteLine("message = "); foreach (var item in pack1.message) { Console.WriteLine(item); } Console.WriteLine(); var list = new List <string>() { pack1.name }; list.AddRange(pack1.message); SendGroupPrivateMessage(pack1.id, pack1.fid, list); break; case 51: var pack2 = JsonConvert.DeserializeObject <FriendMessageEventPack>(task.data); Console.WriteLine("id = " + pack2.id); Console.WriteLine("time = " + pack2.time); Console.WriteLine("name = " + pack2.name); Console.WriteLine("message = "); foreach (var item in pack2.message) { Console.WriteLine(item); } Console.WriteLine(); var list1 = new List <string>() { pack2.name }; list1.AddRange(pack2.message); SendFriendMessage(pack2.id, list1); break; } } Thread.Sleep(10); } catch (Exception e) { ServerMain.LogError(e); } } }); ReadThread = new Thread(() => { while (!IsRun) { Thread.Sleep(100); } DoThread.Start(); byte[] Send; int time = 0; while (IsRun) { try { if (!IsConnect) { ReConnect(); } else if (Socket.Available > 0) { var data = new byte[Socket.Available]; Socket.Receive(data); var type = data[data.Length - 1]; data[data.Length - 1] = 0; QueueRead.Add(new RobotTask { index = type, data = Encoding.UTF8.GetString(data) }); } else if (time >= 20) { time = 0; if (Socket.Poll(1000, SelectMode.SelectRead)) { ServerMain.LogOut("机器人连接中断"); IsConnect = false; } } else if (QueueSend.TryTake(out Send)) { Socket.Send(Send); } time++; Thread.Sleep(50); } catch (Exception e) { ServerMain.LogError("机器人连接失败"); ServerMain.LogError(e); IsConnect = false; ServerMain.LogError("机器人20秒后重连"); Thread.Sleep(20000); ServerMain.LogError("机器人重连中"); } } }); ReadThread.Start(); IsRun = true; ReadTest(); }
public static void Start() { QueueRead = new ConcurrentBag <RobotTask>(); QueueSend = new ConcurrentBag <byte[]>(); DoThread = new Thread(() => { RobotTask task; while (IsRun) { try { if (QueueRead.TryTake(out task)) { switch (task.index) { case 49: var pack = JsonConvert.DeserializeObject <GroupMessageEventPack>(task.data); Console.WriteLine("id = " + pack.id); Console.WriteLine("fid = " + pack.fid); Console.WriteLine("name = " + pack.name); Console.WriteLine("message = " + pack.message); Console.WriteLine(); break; case 50: var pack1 = JsonConvert.DeserializeObject <TempMessageEventPack>(task.data); Console.WriteLine("id = " + pack1.id); Console.WriteLine("fid = " + pack1.fid); Console.WriteLine("name = " + pack1.name); Console.WriteLine("message = " + pack1.message); Console.WriteLine(); break; case 51: var pack2 = JsonConvert.DeserializeObject <FriendMessageEventPack>(task.data); Console.WriteLine("id = " + pack2.id); Console.WriteLine("time = " + pack2.time); Console.WriteLine("name = " + pack2.name); Console.WriteLine("message = " + pack2.message); Console.WriteLine(); break; } } Thread.Sleep(10); } catch (Exception e) { ServerMain.LogError(e); } } }); ReadThread = new Thread(() => { while (!IsRun) { Thread.Sleep(100); } DoThread.Start(); byte[] Send; while (IsRun) { try { if (!IsConnect) { ReConnect(); } else if (Socket.Available > 0) { var data = new byte[Socket.Available]; Socket.Receive(data); var type = data[data.Length - 1]; data[data.Length - 1] = 0; QueueRead.Add(new RobotTask { index = type, data = Encoding.UTF8.GetString(data) }); } else if (Socket.Poll(1000, SelectMode.SelectRead)) { ServerMain.LogOut("机器人连接中断"); IsConnect = false; } else if (QueueSend.TryTake(out Send)) { Socket.Send(Send); } Thread.Sleep(50); } catch (Exception e) { ServerMain.LogError("机器人连接失败"); ServerMain.LogError(e); IsConnect = false; ServerMain.LogError("机器人20秒后重连"); Thread.Sleep(20000); ServerMain.LogError("机器人重连中"); } } }); ReadThread.Start(); IsRun = true; ReadTest(); }