Пример #1
0
 private void btnJoin_Click(object sender, EventArgs e)
 {
     if (txtUserName.Text.Trim() != "")
     {
         try
         {
             NewJoin      += new UserJoined(Form1_NewJoin);
             MessageSent  += new UserSendMessage(Form1_MessageSent);
             RemoveUser   += new UserLeft(Form1_RemoveUser);
             channel       = null;
             this.userName = txtUserName.Text.Trim();
             InstanceContext context = new InstanceContext(new Form1(txtUserName.Text.Trim()));
             factory = new DuplexChannelFactory <IChatChannel>(context, "ChatEndPoint");
             channel = factory.CreateChannel();
             channel.Open();
             channel.Join(this.userName);
             gbChatWindow.Enabled = true;
             gbUserInfo.Enabled   = false;
             this.AcceptButton    = btnSend;
             txtChatMessages.AppendText("******Welcome to Turn Shop Automobile Chat Application****\r\n");
             txtSendMessage.Select();
             txtSendMessage.Focus();
             this.Text = "Chat Clent-" + txtUserName.Text;
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString(), "Error");
         }
     }
 }
Пример #2
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public ResponseModel AddUserSendMessage(AddUserSendMessage sendmessage)
        {
            var u = new UserSendMessage
            {
                UserId            = sendmessage.UserId,
                SendMessageUserId = sendmessage.SendMessageUserId,
                Message           = sendmessage.Message,
                SendTime          = DateTime.Now,
                IfCheck           = 0
            };

            _db.UserSendMessage.Add(u);
            int i = _db.SaveChanges();

            if (i > 0)
            {
                return new ResponseModel {
                           code = 200, result = "消息发送成功"
                }
            }
            ;
            return(new ResponseModel {
                code = 0, result = "消息发送失败"
            });
        }
        private void Bt_LogIn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                try
                {
                    NewJoin     += new UserJoined(ChatClient_NewJoin);
                    MessageSent += new UserSendMessage(ChatClient_MessageSent);
                    RemoveUser  += new UserLeft(ChatClient_RemoveUser);

                    channel       = null;
                    this.userName = txtUserName.Text.Trim();
                    InstanceContext context = new InstanceContext(new MainWindow(txtUserName.Text.Trim()));
                    factory = new DuplexChannelFactory <IChatChannel>(context, "ChatEndPoint");
                    channel = factory.CreateChannel();
                    IOnlineStatus status = channel.GetProperty <IOnlineStatus>();
                    status.Offline += new EventHandler(Offline);
                    status.Online  += new EventHandler(Online);
                    channel.Open();
                    channel.Join(this.userName);
                    rtbMessages.AppendText("*****************************WEL-COME to Chat Application*****************************\r\n");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
Пример #4
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                try
                {
                    // Register an event
                    NewJoin     += new UserJoined(ChatClient_NewJoin);
                    MessageSent += new UserSendMessage
                                       (ChatClient_MessageSent);
                    RemoveUser += new UserLeft(ChatClient_RemoveUser);

                    channel       = null;
                    this.userName = txtUserName.Text.Trim();
                    // Create InstanceContext to handle call back interface
                    // Pass the object of the CallbackContract implementor
                    InstanceContext context = new InstanceContext(
                        new ChatClient(txtUserName.Text.Trim()));
                    // We create a participant with the given end point
                    // The communication is managed with CHAT MESH and
                    // each client creates a duplex
                    // end point with the mesh. Mesh is nothing but the
                    // named collection of nodes.
                    factory =
                        new DuplexChannelFactory <IChatChannel>(context, "ChatEndPoint");
                    channel = factory.CreateChannel();
                    channel.Open();
                    channel.Join(this.userName);
                    grpMessageWindow.Enabled   = true;
                    grpUserList.Enabled        = true;
                    grpUserCredentials.Enabled = false;
                    this.AcceptButton          = btnSend;
                    rtbMessages.AppendText
                        ("****WEL-COME to Chat Application*****\r\n");
                    txtSendMessage.Select();
                    txtSendMessage.Focus();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
Пример #5
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                try
                {
                    NewJoin += new UserJoined(ChatClient_NewJoin);
                    MessageSent += new UserSendMessage(ChatClient_MessageSent);
                    RemoveUser += new UserLeft(ChatClient_RemoveUser);

                    channel = null;
                    this.userName = txtUserName.Text.Trim();
                    InstanceContext context = new InstanceContext(
                        new ChatClient(txtUserName.Text.Trim()));
                    factory =
                        new DuplexChannelFactory<IChatChannel>(context, "ChatEndPoint");
                    channel = factory.CreateChannel();
                    IOnlineStatus status = channel.GetProperty<IOnlineStatus>();
                    status.Offline += new EventHandler(Offline);
                    status.Online += new EventHandler(Online);                    
                    channel.Open();                    
                    channel.Join(this.userName);
                    grpMessageWindow.Enabled = true;
                    grpUserList.Enabled = true;                    
                    grpUserCredentials.Enabled = false;                    
                    this.AcceptButton = btnSend;
                    rtbMessages.AppendText("*****************************WEL-COME to Chat Application*****************************\r\n");
                    txtSendMessage.Select();
                    txtSendMessage.Focus();
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
Пример #6
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                try
                {
                    NewJoin     += new UserJoined(ChatClient_NewJoin);
                    MessageSent += new UserSendMessage(ChatClient_MessageSent);
                    RemoveUser  += new UserLeft(ChatClient_RemoveUser);

                    channel       = null;
                    this.userName = txtUserName.Text.Trim();
                    /**/
                    #region IpAddress
                    string      strHostName = System.Net.Dns.GetHostName();
                    string      IPAddress   = "";
                    IPHostEntry ipEntry     = System.Net.Dns.GetHostEntry(strHostName);

                    foreach (IPAddress ipAddress in ipEntry.AddressList)
                    {
                        if (ipAddress.AddressFamily.ToString() == "InterNetwork")
                        {
                            IPAddress = ipAddress.ToString();
                        }
                    }

                    string Address    = string.Format("net.tcp://{0}/ChatServer", IPAddress);
                    Uri    URiAddress = new Uri(Address, UriKind.RelativeOrAbsolute);

                    #endregion

                    InstanceContext instanceContext = new InstanceContext(new ChatClient(txtUserName.Text.Trim()));



                    ////NetPeerTcpBinding chatBinding = new NetPeerTcpBinding();
                    ////Uri baseAddress = new Uri(Address);
                    ////chatBinding.Port = 0;
                    ////chatBinding.Security.Mode = SecurityMode.None;
                    ////chatBinding.Resolver.Mode = PeerResolverMode.Custom;
                    ////chatBinding.Resolver.Custom.Address = new EndpointAddress(baseAddress);
                    ////chatBinding.Resolver.Custom.Binding = new NetTcpBinding(SecurityMode.None);
                    ////chatBinding.Resolver.Custom.Binding.OpenTimeout = TimeSpan.FromMinutes(5);
                    ////DuplexChannelFactory<IChatChannel> factory = new DuplexChannelFactory<IChatChannel>(instanceContext, chatBinding, "net.p2p://chatMesh/ChatServer");
                    ////channel = factory.CreateChannel();

                    /**/
                    InstanceContext context = new InstanceContext(
                        new ChatClient(txtUserName.Text.Trim()));
                    factory =
                        new DuplexChannelFactory <IChatChannel>(context, "ChatEndPoint");
                    ((System.ServiceModel.NetPeerTcpBinding)(((System.ServiceModel.ChannelFactory)(factory)).Endpoint.Binding)).Resolver.Custom.Address = new EndpointAddress(string.Format("net.tcp://{0}/ChatServer", IPAddress));
                    channel = factory.CreateChannel();
                    /**/

                    IOnlineStatus status = channel.GetProperty <IOnlineStatus>();
                    status.Offline += new EventHandler(Offline);
                    status.Online  += new EventHandler(Online);
                    channel.Open();
                    channel.Join(this.userName);
                    grpMessageWindow.Enabled   = true;
                    grpUserList.Enabled        = true;
                    grpUserCredentials.Enabled = false;
                    this.AcceptButton          = btnSend;
                    rtbMessages.AppendText("'''''''''''''''''''''WEL-COME to Chat Application''''''''''''''''''''''\r\n");
                    txtSendMessage.Select();
                    //txtSendMessage.Focus();
                    cmbProject.Focus();

                    CreateComponentTable();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }