예제 #1
0
        public void WebSocketHandShake(ClientModel cModel, string msg, byte[] data)
        {
            #region 网络WebSocket协议握手
            string webkey = AxTcpServer.GetSecKey(data, data.Length);
            if (!string.IsNullOrEmpty(webkey) && cModel.ClientStyle != ClientStyle.WebSocket)
            {
                byte[] bufferwoshou = AxTcpServer.PackHandShakeData(webkey);
                if (bufferwoshou.Length > 0)
                {
                    cModel.ClientStyle = ClientStyle.WebSocket;
                    cModel.ClientSocket.Send(bufferwoshou);
                }
            }
            else
            {
                //if (cModel.ClientStyle == ClientStyle.WebSocket)
                //{
                //    string webstr = AxTcpServer.AnalyticData(data, data.Length);
                //    msg = webstr;
                //    data = Encoding.Default.GetBytes(msg);
                //}
            }

            #endregion
        }
예제 #2
0
        /// <summary>
        /// 线程池回调
        /// </summary>
        /// <param name="obj"></param>

        public void ClientSocketCallBack(Object obj)
        {
            Socket temp = (Socket)obj;


            #region 承载socket的客户端
            string      ip          = ((IPEndPoint)temp.RemoteEndPoint).Address.ToString();
            string      port        = ((IPEndPoint)temp.RemoteEndPoint).Port.ToString();
            ClientModel ctemp       = ResoultSocket(ip, int.Parse(port));
            byte[]      receivebyte = new byte[1024];
            #endregion
            while (IsStartListening)
            {
                Thread.Sleep(10);
                //MemoryStream mStream = new MemoryStream();
                //mStream.Position = 0;
                try
                {
                    //可自定心跳包数据
                    //temp.Send(System.Text.Encoding.Default.GetBytes("&conn&")); //心跳检测socket连接
                    int bytelen = temp.Receive(receivebyte);


                    if (bytelen > 0)
                    {
                        byte[] bytes = new byte[bytelen];
                        Array.Copy(receivebyte, 0, bytes, 0, bytelen);
                        //OnTcpServerReceviceByte(temp, bytes);
                        //接收客户端数据
                        string clientRecevieStr = ASCIIEncoding.Default.GetString(receivebyte, 0, bytelen);
                        //验证websocket握手协议
                        if (ctemp != null && ctemp.ClientStyle != ClientStyle.WebSocket)
                        {
                            //如果客户端不存在则退出检测
                            WebSocketHandShake(ctemp, clientRecevieStr, bytes);
                        }
                        if (ctemp.ClientStyle == ClientStyle.WebSocket)
                        {
                            string webstr = AxTcpServer.AnalyticData(bytes, bytes.Length);
                            clientRecevieStr = webstr;
                            bytes            = Encoding.Default.GetBytes(clientRecevieStr);
                        }
                        OnTcpServerRecevice(temp, clientRecevieStr, bytes);
                        OnGetLogEnterHead(ctemp, LogType.ReceviedData, DataTool.HexByteArrayToString(bytes));

                        //DelegateHelper.TcpServerReceive(temp, clientRecevieStr);
                    }
                    else if (bytelen == 0)
                    {
                        //接收到数据时数据长度一定是>0,若为0则表示客户端断线
                        //string ip = ((IPEndPoint)temp.RemoteEndPoint).Address.ToString();
                        //string port = ((IPEndPoint)temp.RemoteEndPoint).Port.ToString();
                        //ClientModel ctemp = ResoultSocket(ip, int.Parse(port));
                        if (ctemp != null)
                        {
                            ctemp.ClientSocket.Shutdown(SocketShutdown.Both);
                            OnTcpServerOfflineClientEnterHead(ctemp);
                            ClientSocketList.Remove(ctemp);
                            OnTcpServerStateInfoEnterHead("<" + ip + ":" + port + ">---下线",
                                                          SocketState.ClientOnOff);
                            OnTcpServerOfflineClientEnterHead(ctemp);
                            OnTcpServerReturnClientCountEnterHead(ClientSocketList.Count);
                        }
                        try
                        {
                            temp.Shutdown(SocketShutdown.Both);
                        }
                        catch
                        {
                        }
                        break;
                    }
                }
                catch
                {
                    try
                    {
                        if (ctemp != null)
                        {
                            ctemp.ClientSocket.Shutdown(SocketShutdown.Both);
                            OnTcpServerOfflineClientEnterHead(ctemp);
                            ClientSocketList.Remove(ctemp);
                            OnTcpServerStateInfoEnterHead("<" + ip + ":" + port + ">---下线",
                                                          SocketState.ClientOnOff);
                            OnTcpServerOfflineClientEnterHead(ctemp);
                            OnTcpServerReturnClientCountEnterHead(ClientSocketList.Count);
                        }
                    }
                    catch
                    {
                    }
                    break;
                }
            }
        }