HTML5技术

急急如律令!火速搭建一个即时通讯系统!(附源码分享——高度可移植!) - CXLian(4)

字号+ 作者:H5之家 来源:博客园 2016-01-26 18:01 我要评论( )

1.客户端A发送聊天消息给服务器 button_send_Click( object sender, EventArgs e) 7 { 8 string chatText = this .richTextBox_Write.Text; 9 if ( string .IsNullOrEmpty(chatText)) 10 { ); 12 return ; 13 } 14

                               

1.客户端A发送聊天消息给服务器

button_send_Click(object sender, EventArgs e) 7 { 8 string chatText = this.richTextBox_Write.Text; 9 if (string.IsNullOrEmpty(chatText)) 10 { ); 12 return; 13 } 14 ChatMsg chatMsg = new ChatMsg(this.selfUserID, this.friendID, chatText); 15 this.tcpPassiveEngine.SendMessageToServer(chatMsg.ToContractStream()); 16 this.ShowChatMsg(chatMsg); 17 }

View Code

2.服务端转发聊天消息

1 if (msgType == MsgType.Chatting) 2 { 3 ChatMsg chatMsg = MsgHelper.DeserializeMsg<ChatMsg>(msg); 4 if (this.onlineManager.GetKeyList().Contains(chatMsg.TargetUserID)) 5 { 6 IPEndPoint targetUserAddress = this.onlineManager.Get(chatMsg.TargetUserID).Address; 7 this.tcpServerEngine.SendMessageToClient(targetUserAddress, msg); 8 } 9 }

View Code

3.客户端B接收并显示聊天消息 

1 void tcpPassiveEngine_MessageReceived(IPEndPoint userAddress, byte[] msg) 2 { msgType = BitConverter.ToInt32(msg, 0); (msgType == MsgType.Chatting) 7 { 8 ChatMsg chatMsg = MsgHelper.DeserializeMsg<ChatMsg>(msg); 9 this.ShowChatForm(chatMsg.SourceUserID); 10 this.ChatMsgReceived(chatMsg); 11 } 12 } 显示聊天窗 ShowChatForm(string friendUserID) 19 { 20 if (this.InvokeRequired) 21 { 22 this.Invoke(new CbGeneric<string>(this.ShowChatForm), friendUserID); 23 } { 26 ChatForm form = this.chatFormManager.GetForm(friendUserID); 27 if (form == null) 28 { 29 form = new ChatForm(this.selfID, friendUserID, this, this.tcpPassiveEngine); , friendUserID); 31 this.chatFormManager.Add(form); 32 form.Show(); 33 } 34 form.Focus(); 35 } 36 } 显示聊天消息 ShowChatMsg(ChatMsg chatMsg) 44 { 45 if (this.InvokeRequired) 46 { 47 this.Invoke(new CbGeneric<ChatMsg>(this.formMain_chatMsgReceived), chatMsg); 48 } { + chatMsg.TimeSent.ToString() + ); ); 53 this.richTextBox_Write.Clear(); 54 } 55 }

View Code

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • vue2.0版cnode社区项目搭建及实战开发 - sandisen

    vue2.0版cnode社区项目搭建及实战开发 - sandisen

    2017-04-20 14:00

  • net.sz.framework 框架 轻松搭建服务---让你更专注逻辑功能---初探 - 失足程序员

    net.sz.framework 框架 轻松搭建服务---让你更专注逻辑功能---初探 -

    2017-04-02 10:11

  • Ionic2 开发笔记(1)ionic2 +angular2搭建 - 早上~得~喝粥

    Ionic2 开发笔记(1)ionic2 +angular2搭建 - 早上~得~喝粥

    2017-03-13 16:00

  • React+webpack开发环境的搭建 - 雨和雪

    React+webpack开发环境的搭建 - 雨和雪

    2017-03-11 09:05

网友点评
)