Beispiel #1
0
            /// <summary>
            /// 接受用户消息
            /// </summary>
            private void ReceiveNetMessage()
            {
                byte[]   bytes    = new byte[2048];
                EndPoint endPoint = new IPEndPoint(IPAddress.Any, 44478);

                while (true)
                {
                    try
                    {
                        this.messageSocket.ReceiveFrom(bytes, ref endPoint);
                    }
                    catch (Exception)
                    {
                        break;
                    }

                    IPEndPoint end = endPoint as IPEndPoint;

                    string message = Method.RemoveEndstrString(Encoding.UTF8.GetString(bytes));
                    if (null == message)
                    {
                        continue;
                    }
                    if (end.Address.ToString().Equals(KeyData.StaticInfo.MyUser.IP))
                    {
                        continue;
                    }
                    try
                    {
                        Model.Transmission.NetInfo info = JsonConvert.DeserializeObject <Model.Transmission.NetInfo>(message);
                        switch (info.typeID)
                        {
                        case Model.Transmission.NET_MESSAGEID:
                            Model.Transmission.NetMessage data1 = JsonConvert.DeserializeObject <Model.Transmission.NetMessage>(info.JsonString);
                            data1.isPublic = end.Port == sendProt_Pub;
                            if (this.ReceiveUserMessageEvent != null)
                            {
                                this.ReceiveUserMessageEvent.Invoke(data1);
                            }
                            break;

                        case Model.Transmission.NET_EVENTMESSAGE:
                            Model.Transmission.NetEventMessage data2 = JsonConvert.DeserializeObject <Model.Transmission.NetEventMessage>(info.JsonString);
                            data2.isPublic = end.Port == sendProt_Pub;
                            if (this.ReceiveEventMessageEvent != null)
                            {
                                this.ReceiveEventMessageEvent.Invoke(data2);
                            }
                            break;

                        case Model.Transmission.NET_FILEDATAID:
                            Model.Transmission.NetFileData data = JsonConvert.DeserializeObject <Model.Transmission.NetFileData>(info.JsonString);
                            data.isPublic = end.Port == sendProt_Pub;
                            data.message  = "文件:[" + data.filename + "]";
                            if (this.ReceiveFileMessageEvent != null)
                            {
                                ReceiveFileMessageEvent.Invoke(data);
                            }
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }