Example #1
0
        private void btn_send_Click(object sender, EventArgs e)
        {
            JObject ToSend = new JObject();

            ToSend["Command"]        = DateTime.Now.ToString("TalkTo");
            ToSend["MessageTime"]    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ToSend["Content"]        = tb_content.Text;
            ToSend["TalkToUserName"] = TalkingUserName;
            MessageLib.SocketMessage sm = new MessageLib.SocketMessage(tcpc);
            sm.SendContent(ToSend);
        }
Example #2
0
        private void OnAcceptDo(TcpClient tc)
        {
            OnLineData newc = new OnLineData();

            newc.IP   = ((IPEndPoint)tc.Client.RemoteEndPoint).Address.ToString();
            newc.Port = ((IPEndPoint)tc.Client.RemoteEndPoint).Port;
            newc.tcpl = tc;

            this.Invoke(new Action(() =>
            {
                OnLineInf.Add(newc);
            }));


            SocketError se = new SocketError();

            MessageLib.SocketMessage sm = new MessageLib.SocketMessage(tc);
            sm.OnReceiveMessage += sm_OnReceiveMessage;
            sm.OnReceiveError   += sm_OnReceiveError;
            sm.OnSendError      += sm_OnSendError;
            IAsyncResult R = tc.Client.BeginReceive(sm.MessageIndex, 0, 4, SocketFlags.None, out se, sm.SizeReceiveCallBack, tc);
        }
Example #3
0
        private void StartTalk(string IP, Int32 Port)
        {
            if (tcpc == null)
            {
                tcpc = new TcpClient();
                tcpc.Connect(IPAddress.Parse(IP), Port);
            }



            JObject ToSend = new JObject();

            ToSend["UserName"] = tb_username.Text;
            ToSend["Pwd"]      = tb_password.Text;
            ToSend["Command"]  = "Login";
            MessageLib.SocketMessage sm = new MessageLib.SocketMessage(tcpc);
            sm.OnReceiveMessage += sm_OnReceiveMessage;
            sm.OnSendError      += sm_OnSendError;
            sm.OnReceiveError   += sm_OnReceiveError;
            SocketError  se = new SocketError();
            IAsyncResult R  = tcpc.Client.BeginReceive(sm.MessageIndex, 0, 4, SocketFlags.None, out se, sm.SizeReceiveCallBack, tcpc);

            sm.SendContent(ToSend);
        }
Example #4
0
        /// <summary>
        /// 主要的消息处理逻辑
        /// </summary>
        /// <param name="newmessage"></param>
        /// <param name="ReceiveTc"></param>
        /// <param name="Receiveskm"></param>
        void sm_OnReceiveMessage(Newtonsoft.Json.Linq.JObject newmessage, TcpClient ReceiveTc, MessageLib.SocketMessage Receiveskm)
        {
            if (newmessage["Command"].ToString() == "Login")
            {
                string         UserName = newmessage["UserName"].ToString();
                string         Pwd      = newmessage["Pwd"].ToString();
                bool           LoginR   = System.Web.Security.Membership.ValidateUser(UserName, Pwd);
                JObject        r        = new JObject();
                MembershipUser msr      = Membership.GetUser(UserName);
                if (msr == null)
                {
                    r["Status"] = "用户名" + UserName + ",不存在";
                }

                else if (msr.IsLockedOut == true)
                {
                    r["Status"] = "用户名" + UserName + ",密码错误次数太多,需由管理员处理";
                }
                else if (LoginR == false)
                {
                    r["Status"] = "用户密码不符";
                }
                else
                {
                    r["Status"] = "Success";


                    #region "多处登录,踢出"
                    OnLineData ToKill = OnLineInf.SingleOrDefault(t => t.UserID == (Guid)msr.ProviderUserKey);
                    if (ToKill != null)
                    {
                        JObject sr = new JObject();
                        sr["Status"] = "账号已在其他地方登录";
                        ToKill.skm.SendContent(sr);
                    }
                    #endregion

                    OnLineData ToUpd = OnLineInf.SingleOrDefault(t => t.tcpl == ReceiveTc);

                    ToUpd.UserID   = (Guid)msr.ProviderUserKey;
                    ToUpd.UserName = msr.UserName;



                    ServerData.dbDataContext db = new ServerData.dbDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
                    r["Command"] = "ImLogin";
                    //取得好友
                    var Friends = db.aspnet_Friendship.Where(t => t.aspnet_UserID == (Guid)msr.ProviderUserKey);
                    r["Friends"] = new JArray();
                    foreach (var Frienditem in Friends)
                    {
                        MembershipUser msu     = Membership.GetUser(Frienditem.FriendaspnetID);
                        JObject        JFriend = new JObject();
                        JFriend["UserName"] = msu.UserName.ToLower();

                        OnLineData old = OnLineInf.SingleOrDefault(t => t.UserID == (Guid)msu.ProviderUserKey);
                        if (old != null)
                        {
                            JFriend["IP"]     = old.IP;
                            JFriend["Port"]   = old.Port;
                            JFriend["OnLine"] = true;
                        }
                        else
                        {
                            JFriend["IP"]     = "";
                            JFriend["Port"]   = "";
                            JFriend["OnLine"] = false;
                        }



                        (r["Friends"] as JArray).Add(JFriend);
                    }
                    //通知好友上线
                    var belFriends = db.aspnet_Friendship.Where(t => t.FriendaspnetID == (Guid)msr.ProviderUserKey);
                    foreach (var Frienditem in belFriends)
                    {
                        OnLineData tonotice = OnLineInf.SingleOrDefault(t => t.UserID == Frienditem.aspnet_UserID);
                        if (tonotice != null)
                        {
                            JObject ToSendMessage = new JObject();
                            ToSendMessage["Status"]         = "Success";
                            ToSendMessage["Command"]        = "FriendLogin";
                            ToSendMessage["FriendUserName"] = msr.UserName.ToLower();
                            ToSendMessage["IP"]             = ((IPEndPoint)ReceiveTc.Client.RemoteEndPoint).Address.ToString();
                            tonotice.skm.SendContent(ToSendMessage);
                        }
                    }
                }
                Receiveskm.SendContent(r);
                this.Invoke(new Action(() => {
                    this.Refresh();
                }));
                ;
            }
            //服务器转发通讯
            else if (newmessage["Command"].ToString() == "TalkTo")
            {
                MembershipUser tomsr   = Membership.GetUser(newmessage["TalkToUserName"].ToString());
                OnLineData     old     = OnLineInf.SingleOrDefault(t => t.UserID == (Guid)tomsr.ProviderUserKey);
                OnLineData     SayFrom = OnLineInf.SingleOrDefault(t => t.tcpl == ReceiveTc);
                if (old != null)
                {
                    newmessage["Command"]          = "TalkFrom";
                    newmessage["Status"]           = "Success";
                    newmessage["TalkFromUserName"] = SayFrom.UserName;
                    old.skm.SendContent(newmessage);
                }
                else
                {
                    JObject rtnr = new JObject();
                    rtnr["Command"]  = "TalkToError";
                    rtnr["Status"]   = "Success";
                    rtnr["UserName"] = newmessage["TakeToUserName"].ToString();
                    rtnr["Content"]  = newmessage["Content"].ToString();
                    Receiveskm.SendContent(rtnr);
                }
            }
            //获取P2P的UDP端口
            else if (newmessage["Command"].ToString() == "GetUDPHoleInf")
            {
                MembershipUser tomsr  = Membership.GetUser(newmessage["UserName"].ToString());
                OnLineData     tofind = OnLineInf.SingleOrDefault(t => t.UserID == (Guid)tomsr.ProviderUserKey);
                JObject        tor    = new JObject();
                tor["Command"] = "GetUDPHoleInf";
                tor["Status"]  = "Success";
                tor["P2PPort"] = tofind == null ? "" : tofind.P2pPort.ToString();
                Receiveskm.SendContent(tor);
            }
        }
Example #5
0
 static void sm_OnReceiveError(JObject newmessage, TcpClient tc, MessageLib.SocketMessage skm)
 {
     throw new NotImplementedException();
 }
Example #6
0
        /// <summary>
        /// 主要的消息处理逻辑
        /// </summary>
        /// <param name="newmessage"></param>
        /// <param name="tc"></param>
        /// <param name="skm"></param>
        void sm_OnReceiveMessage(JObject newmessage, TcpClient tc, MessageLib.SocketMessage skm)
        {
            if (newmessage["Status"].ToString() != "Success")
            {
                MessageBox.Show("登录失败," + newmessage["Status"].ToString());
            }
            else
            {
                this.Invoke(new Action(() =>
                {
                    Btn_Start.Enabled = false;
                }));


                if (newmessage["Command"].ToString() == "ImLogin")
                {
                    foreach (var item in newmessage["Friends"])
                    {
                        this.Invoke(new Action(() =>
                        {
                            Friend fr = new Friend();
                            fr.IP     = item["IP"].ToString();
                            if (fr.IP != "")
                            {
                                fr.OnLine = "是";
                            }
                            else
                            {
                                fr.OnLine = "否";
                            }
                            fr.UserName = item["UserName"].ToString();
                            data_Friends.Add(fr);
                            this.Refresh();
                        }));
                    }
                }
                else if (newmessage["Command"].ToString() == "TalkFrom")
                {
                    DataRow dr = data_TalkingMessage.NewRow();
                    dr.SetField("RawJson", newmessage);
                    dr.SetField("UserName", newmessage["TalkFromUserName"].ToString());
                    dr.SetField("Content", newmessage["Content"].ToString());
                    dr.SetField("MessageTime", Convert.ToDateTime(newmessage["MessageTime"].ToString()));
                    data_TalkingMessage.Rows.Add(dr);
                    this.Invoke(new Action(() => {
                        this.Refresh();
                    }));
                }
                else if (newmessage["Command"].ToString() == "TalkToError")
                {
                    MessageBox.Show("与" + newmessage["UserName"] + "的聊天内容:" + Environment.NewLine + newmessage["Content"] + Environment.NewLine + "对方没有收到");
                }
                else if (newmessage["Command"].ToString() == "FriendLogin")
                {
                    this.Invoke(new Action(() =>
                    {
                        Friend fnd = data_Friends.SingleOrDefault(t => t.UserName == newmessage["FriendUserName"].ToString());
                        fnd.IP     = newmessage["IP"].ToString();
                        fnd.OnLine = "是";
                        this.Refresh();
                    }));
                }
                //请求UDP开洞(P2P传输)
                else if (newmessage["Command"].ToString() == "OpenHole")
                {
                    udpc = new UdpClient();
                    this.Invoke(new Action(() =>
                    {
                        udpc.Connect(IPAddress.Parse(tb_ip.Text), Convert.ToInt32(tb_port.Text));
                        JObject SendInf    = new JObject();
                        SendInf["Command"] = "UpdateUDF";
                        byte[] buf         = Encoding.UTF8.GetBytes(SendInf.ToString());
                        udpc.Send(BitConverter.GetBytes(buf.Length), 4);
                        udpc.Send(buf, buf.Length);
                    }));
                }
                //P2P连接
                else if (newmessage["Command"].ToString() == "P2PConnectTo")
                {
                    udpc = new UdpClient();
                }
            }
        }