|
| 1 | +using NetworkSocket; |
| 2 | +using NetworkSocket.Plugs; |
| 3 | +using NetworkSocket.WebSocket; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | +using System; |
| 7 | +using System.Collections; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Text; |
| 10 | + |
| 11 | +namespace AMG_Cloud |
| 12 | +{ |
| 13 | + public class WebSocketServerMiddleware : WebSocketMiddlewareBase |
| 14 | + { |
| 15 | + protected sealed override void OnBinary(IContenxt context, FrameRequest frame) |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + var text = Encoding.UTF8.GetString(frame.Content); |
| 20 | + var jsonResult = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(text); |
| 21 | + var ipMessage = jsonResult["ipMessage"]; |
| 22 | + //var uip = ((System.Net.IPEndPoint)context.Session.RemoteEndPoint).Address.ToString(); |
| 23 | + var uuip = context.Session.RemoteEndPoint.ToString(); |
| 24 | + var hostname = ((System.Net.IPEndPoint)context.Session.RemoteEndPoint).Address.ToString(); |
| 25 | + if (jsonResult["hostName"] != null) |
| 26 | + { |
| 27 | + hostname = jsonResult["hostName"].ToString(); |
| 28 | + } |
| 29 | + //预处理IP |
| 30 | + var IPMessage = new Dictionary<string, string>(); |
| 31 | + foreach (JProperty jp in ipMessage) |
| 32 | + { |
| 33 | + var ip = hostname + " : " + jp.Name; |
| 34 | + var request = jp.Value.ToString(); |
| 35 | + if (IPMessage.ContainsKey(ip)) |
| 36 | + { |
| 37 | + IPMessage[ip] = request; |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + IPMessage.Add(ip, request); |
| 42 | + } |
| 43 | + } |
| 44 | + //Console.WriteLine(uip); |
| 45 | + //处理IP主机 |
| 46 | + if (Globle.ServeIPMessage.ContainsKey(uuip)) |
| 47 | + { |
| 48 | + Globle.ServeIPMessage[uuip] = IPMessage; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + Globle.ServeIPMessage.Add(uuip, IPMessage); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + //var session = (WebSocketSession)context.Session.Wrapper; |
| 57 | + //session.SendBinary(byteArray); |
| 58 | + //item.SendBinary(byteArray); |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + var log = "[WSS]服务端发生错误 " + ex.Message + ":" + ex.StackTrace; |
| 63 | + Console.WriteLine(log); |
| 64 | + //Globle.AddDataLog(log); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public class CustomWSServerPlug : PlugBase |
| 70 | + { |
| 71 | + protected sealed override void OnConnected(object sender, IContenxt context) |
| 72 | + { |
| 73 | + var log = string.Format("[WSS]时间:{0} 用户:{1} 连接", DateTime.Now.ToString("mm:ss"), context.Session.ToString()); |
| 74 | + //Debug.Log(log); |
| 75 | + Console.WriteLine(log); |
| 76 | + } |
| 77 | + |
| 78 | + protected sealed override void OnDisconnected(object sender, IContenxt context) |
| 79 | + { |
| 80 | + var log = string.Format("[WSS]时间:{0} 用户:{1} 断开连接", DateTime.Now.ToString("mm:ss"), context.Session.RemoteEndPoint.ToString()); |
| 81 | + //var uip = ((System.Net.IPEndPoint)context.Session.RemoteEndPoint).Address.ToString(); |
| 82 | + var uuip = context.Session.RemoteEndPoint.ToString(); |
| 83 | + Globle.ServeIPMessage.Remove(uuip); |
| 84 | + Console.WriteLine(log); |
| 85 | + //Debug.Log(log); |
| 86 | + /*var RemoteIPMessage = Globle.RemoteIPMessage; |
| 87 | + foreach (KeyValuePair<string, string> kvp in RemoteIPMessage) |
| 88 | + { |
| 89 | + if (kvp.Key.IndexOf(context.Session.RemoteEndPoint.ToString()) > 0) |
| 90 | + { |
| 91 | + Globle.RemoteIPMessage.Remove(kvp.Key); |
| 92 | + } |
| 93 | + } |
| 94 | + Globle.AddDataLog(log); |
| 95 | + Globle.globleIPChanged = true;*/ |
| 96 | + //要处理 |
| 97 | + } |
| 98 | + |
| 99 | + protected sealed override void OnException(object sender, Exception exception) |
| 100 | + { |
| 101 | + var log = string.Format("[WSS]时间:{0} 发生错误:{1} {2}", DateTime.Now.ToString("mm:ss"), exception.Message, exception.StackTrace); |
| 102 | + //Debug.Log(log); |
| 103 | + //Globle.AddDataLog(log); |
| 104 | + //Globle.IPMessage.Remove(context.Session.RemoteEndPoint.ToString()); |
| 105 | + Console.WriteLine(log); |
| 106 | + } |
| 107 | + |
| 108 | + /*protected sealed override void OnRequested(object sender, IContenxt context) |
| 109 | + { |
| 110 | +
|
| 111 | + var request = context.StreamReader.ReadString(Encoding.ASCII).Replace("\n", ""); |
| 112 | + var log = string.Format("[WSS]时间:{0} 用户:{1} 信息:{2}", DateTime.Now.ToString("mm:ss"), context.Session.RemoteEndPoint.ToString(), request); |
| 113 | + //Debug.Log(log); |
| 114 | + Console.WriteLine(log); |
| 115 | + }*/ |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +} |
0 commit comments