未来交互新形态:WebUI语音合成正在改变用户体验
2026/1/9 22:51:07
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceAOI外观检测软件.Communicate{/// <summary>/// 通讯服务类/// </summary>publicclassCommunicateService{// 单例模式:使类成为全局唯一的存在//优点1:方便使用//优点2:节省资源//单例模式://1. 私有静态变量(在第四步供外界使用),创建类的实例//2. 私有构造函数,确保外部无法直接实例化(确保是单个实例)//3. 确定供外界调用的代码资源//4. 公开静态属性,供外界使用(把第一步类的实例,开放出去)//5. 外界使用//1. 私有静态变量(在第四步供外界使用),创建类的实例// static 静态: 类 方法 等 (1)在程序启动之前就生成,(2)在程序结束之后,才会消失,(3)全局唯一的存在。privatestaticCommunicateServiceintance=newCommunicateService();//2. 私有构造函数,确保外部无法直接实例化(确保是单个实例)// private 修饰 就变成私有的 ,不允许外界调用,确保单例模式,唯一的特性privateCommunicateService(){}//4. 公开静态属性,供外界使用(把第一步类的实例,开放出去)// public static 构建了开发publicstaticCommunicateServiceInstance{get{returnintance;}}//3. 确定供外界调用的代码资源HttpServerserver=newHttpServer();/// <summary>/// 启动服务端/// </summary>/// <returns></returns>publicboolStart(stringip,intport){server.IP=ip;server.Port=port;if(server.Start()){returntrue;}else{returnfalse;}}/// <summary>/// 发送数据/// </summary>/// <param name="content"></param>publicvoidSend(stringcontent){server.SendMsg(content);}}}usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingAOI外观检测软件.Camera;namespaceAOI外观检测软件.Communicate{/// <summary>/// 服务端/// </summary>publicclassHttpServer{// Http-Server(服务端):// 1:创建socket()// 2:设置IP和端口// 3. 绑定ip和端口// 4:listen()监听,确定能连接多少个客户端// 5:accept()函数接受客户端的连接// 6:接受数据// 7: 发送数据// 8:终止连接。// 1:创建socket()publicSocketSocketSever;// 创建接受客户端的字典(就是成对放的数组),发送数据给客户端的时候要用publicDictionary<string,Socket>CurrentClientlist=newDictionary<string,Socket>();#region属性/// <summary>/// 服务IP/// </summary>publicstringIP{get;set;}/// <summary>/// 端口/// </summary>publicintPort{get;set;}#endregion/// <summary>/// 启动/// </summary>publicboolStart(){try{// 1:创建socket()SocketSever=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);// 2:设置IP和端口。IPEndPointipe=newIPEndPoint(IPAddress.Parse(IP),Port);try{// 3. 绑定ip和端口SocketSever.Bind(ipe);}catch(Exceptionex){MessageBox.Show("服务器开启失败:"+ex.Message);returnfalse;}// 4:listen(),确定能连接多少个客户端: 10 允许最多10个连接在队列中等待SocketSever.Listen(10);// 5.创建一个监听的线程Task.Run(newAction(()=>{AcceptClients();}));returntrue;}catch(Exceptionexp){MessageBox.Show("服务器开启失败:"+exp.Message);returnfalse;}}/// <summary>/// 监听客户端连接/// </summary>publicvoidAcceptClients(){while(true){// 5:accept()函数接受客户端的连接SocketsocketClient=SocketSever.Accept();stringclient=socketClient.RemoteEndPoint.ToString();// 将客户端保存起来CurrentClientlist.Add(client,socketClient);// 6:接受数据Task.Run(newAction(()=>{ReceiveMessage(socketClient);}));}}/// <summary>/// 监听接收客户端数据/// </summary>/// <param name="socketClient"></param>privatevoidReceiveMessage(SocketsocketClient){while(true){// 创建一个缓冲区byte[]buffer=newbyte[1024*1024*10];// 数据长度intlength=-1;stringclient=socketClient.RemoteEndPoint.ToString();try{length=socketClient.Receive(buffer);}catch(Exception){MessageBox.Show(client+"下线了");CurrentClientlist.Remove(client);break;}if(length>0){stringmsg=string.Empty;// 以utf8的格式接受msg=Encoding.UTF8.GetString(buffer,0,length);//MessageBox.Show("接受信息:"+msg);// 触发拍照(在上位机或者PLC发送这个通讯信息的时候,我们进行解析以后,进行拍照)CameraService.Instance.SnapImage();// 显示接受内容。需要使用Invoke,跨线程,跨UI//Invoke(new Action(() =>//{// rtb_Receive_server.AppendText(msg + "\n");//}));}else{MessageBox.Show(client+"下线了");CurrentClientlist.Remove(client);break;}}}/// <summary>/// 发送数据utf8/// </summary>/// <param name="sender"></param>/// <param name="e"></param>publicvoidSendMsg(stringContent){// 获取信息byte[]sendMsg=Encoding.UTF8.GetBytes(Content);// 对客户端发送信息foreach(variteminthis.CurrentClientlist){// 发送数据item.Value?.Send(sendMsg);}}}}