团结如何实现聊天室功能

  介绍

这篇文章给大家分享的是有关团结如何实现聊天室功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

聊天室服务器

服务器需要有以下几个步骤

1,确定套接字协议类型(采用TCP协议或者UDP协议)
2,绑定服务器的IP地址和端口号
3,设置最大监听数量
4,等到连接并处理消息

由于服务器属于一对多的处理关系,因为我们需要用线程来监听消息:

class 客户端   ,{   private 才能;Socket  clientSocket;   private 才能Thread  t;   public 才能;bool 连接   {才能   ,才能得到的   ,,{   ,,,return  clientSocket.Connected;   ,,}   ,,}   private 才能byte [], data =, new 字节[1024];//数据容器   public 才能;客户机(Socket 客户端)   {才能   ,,clientSocket =,客户端;   ,,//启动一个线程,处理客户端的接受   ,,t =, new 线程(ReceiveMsg);   ,,t.Start ();   ,,}      private 才能;void  ReceiveMsg ()   {才能   ,,while (真正的)   ,,{   ,,,//在接收数据之前,判断插座连接是否断开   ,,,if  (clientSocket.Connected !)   ,,,{   ,,,,clientSocket.Close ();   ,,,,打破;//跳出循环终止线程的执行   ,,,}   ,,,int 长度=clientSocket.Receive(数据);   ,,,string  msg =, Encoding.UTF8.GetString(数据,,0,,长度);   ,,,//服务端接收数据后,要将数据分发到客户端   ,,,//广播消息   ,,,Program.BroadcastMsg(味精);   ,,}   ,,}      public 才能;void  SendMsg (string 味精)   {才能   ,,byte [], data =, Encoding.UTF8.GetBytes(味精);   ,,clientSocket.Send(数据);   ,,}      ,,   以前,}

服务器主要代码:

class 程序   ,{   static 才能;List< Client>, clients =, new  List ();//本才能机IP: 192.168.100.172   static 才能;void  Main (string [], args)   {才能   ,,Socket  tcpServer =, new 插座(AddressFamily.InterNetwork, SocketType.Stream,, ProtocolType.Tcp);   ,,tcpServer.Bind (new  IPEndPoint (IPAddress.Parse (“192.168.100.172"), 7788));   ,,tcpServer.Listen (5000);   ,,Console.WriteLine (“Server  .......“运行);   ,,while (真正的)   ,,{   ,,,var  clientSocket =, tcpServer.Accept ();   ,,,Console.WriteLine(“建立连接“);   ,,,Client  Client =, new 客户机(clientSocket);   ,,,clients.Add(客户端);   ,,}   ,,}      public 才能static  void  BroadcastMsg (string 味精)   {才能   ,,var  noConnecteds =, new  List ();   ,,foreach  (var  client 拷贝客户)   ,,{   ,,,if  (client.Connected)   ,,,{   ,,,,client.SendMsg(味精);   ,,,}   ,,,   ,,,{   ,,,,noConnecteds.Add(客户端);   ,,,}   ,,}   ,,foreach  (var  del  noConnecteds拷贝)   ,,{   ,,,clients.Remove (del);   ,,}   ,,}   }

<>强统一客户端代码

统一客户端代码就十分简单,监听服务器的消息并显示到界面上即可

using  System.Collections;   using  System.Collections.Generic;   using  UnityEngine;   using  System.Net;   using  System.Net.Sockets;   using 包含;   using  UnityEngine.UI;   using  System.Threading;   public  class  ChatManager : MonoBehaviour   {   ,public  string  IP =,“192.168.100.172";   ,public  int  Port =, 7788;   ,private  Socket 客户端;   ,private  Thread  t;   ,public  InputField 输入;   ,public  Button  sendBtn;   ,public  Text 项目;      ,public  string 名称;      ,private  string 味精=string.Empty;   ,//Start  is  nbsp; before 从而first  frame 更新   ,void  Start ()   ,{   ConnectedToServer才能();   sendBtn.onClick.AddListener才能((),=祝辞,{   ,,SendMsg (input.text);   ,,input.text =, string.Empty;   ,,});   ,}      ,//Update  is  nbsp; once  per 框架   ,void 更新()   ,{//才能由于在团结中不允许在线程中调用UnityAPI,因此需要的更新中刷新显示   if 才能;(! string.IsNullOrEmpty(味精))   {才能   ,,item.text  +=,“\ n", +,味精;   ,,msg =, string.Empty;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

团结如何实现聊天室功能