/// <summary> /// 握手成功手的连接 /// </summary> /// <param name="sender"></param> /// <param name="me"></param> private void Ci_NewUserConnection(object sender, MessageEntity me) { ClientSocketInstance ci = sender as ClientSocketInstance; BroadcastMessage += new BroadcastEvent(ci.SendMessage); callback.NewUserConnectionJoin(ci, me); }
/// <summary> /// 启动等待连接 /// </summary> public void StartConnection() { serverListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); // Start the socket string[] ip = WebSocketProtocol.GetInstance.ServerId.Split('.'); IPAddress localIp = new IPAddress(new byte[] { Convert.ToByte(ip[0]), Convert.ToByte(ip[1]), Convert.ToByte(ip[2]), Convert.ToByte(ip[3]) }); serverListener.Bind(new IPEndPoint(localIp, WebSocketProtocol.GetInstance.ServerPort)); serverListener.Listen(WebSocketProtocol.GetInstance.ConnectionsCount); while (true) { //等待客户端请求 Socket sc = serverListener.Accept(); if (sc != null) { Thread.Sleep(100); ClientSocketInstance ci = new ClientSocketInstance(); ci.ClientSocket = sc; //初始化三个事件 ci.NewUserConnection += new ClientSocketEvent(Ci_NewUserConnection); ci.ReceiveData += new ClientSocketEvent(Ci_ReceiveData); ci.DisConnection += new ClientSocketEvent(Ci_DisConnection); //开始与客户端握手[握手成功,即可通讯了] ci.ClientSocket.BeginReceive(ci.receivedDataBuffer, 0, ci.receivedDataBuffer.Length, 0, new AsyncCallback(ci.StartHandshake), ci.ClientSocket.Available); listConnection.Add(ci); } } }
/// <summary> /// 启动等待连接 /// </summary> public void StartConnection() { serverListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); // Start the socket string[] ip = WebSocketProtocol.GetInstance.ServerId.Split('.'); IPAddress localIp = new IPAddress(new byte[] { Convert.ToByte(ip[0]), Convert.ToByte(ip[1]), Convert.ToByte(ip[2]), Convert.ToByte(ip[3]) }); serverListener.Bind(new IPEndPoint(localIp, WebSocketProtocol.GetInstance.ServerPort)); serverListener.Listen(WebSocketProtocol.GetInstance.ConnectionsCount); while (true) { //等待客户端请求 Socket sc = serverListener.Accept(); if (sc != null) { Thread.Sleep(100); ClientSocketInstance ci = new ClientSocketInstance(); ci.ClientSocket = sc; //初始化三个事件 ci.NewUserConnection += new ClientSocketEvent(Ci_NewUserConnection); ci.ReceiveData += new ClientSocketEvent(Ci_ReceiveData); ci.DisConnection += new ClientSocketEvent(Ci_DisConnection); //开始与客户端握手[握手成功,即可通讯了] ci.ClientSocket.BeginReceive(ci.receivedDataBuffer, 0, ci.receivedDataBuffer.Length, 0, new AsyncCallback(ci.StartHandshake), ci.ClientSocket.Available); listConnection.Add(ci); } } }