Пример #1
0
        private void InitServer()
        {
            _tcpSocketServer.HandleServerStarted = theServer =>
            {
                HandleServerStarted?.Invoke(this);
            };

            _tcpSocketServer.HandleException = ex =>
            {
                HandleException?.Invoke(ex);
            };

            _tcpSocketServer.HandleClientClose = (theServer, theCon) =>
            {
                var webCon = GetConnection(theCon.ConnectionId);
                CloseConnection(webCon);
                HandleClientClose?.Invoke(this, webCon);
            };

            _tcpSocketServer.HandleNewClientConnected = (theServer, theCon) =>
            {
                WebSocketConnection newCon = new WebSocketConnection(this, theCon)
                {
                    HandleClientClose = HandleClientClose == null ? null : new Action <WebSocketServer, WebSocketConnection>(HandleClientClose),
                    HandleSendMsg     = HandleSendMsg == null ? null : new Action <WebSocketServer, WebSocketConnection, string>(HandleSendMsg)
                };

                AddConnection(newCon);

                HandleNewClientConnected?.Invoke(this, newCon);
            };

            _tcpSocketServer.HandleRecMsg = (thServer, theCon, bytes) =>
            {
                string recStr = bytes.ToString(Encoding.UTF8);
                if (IsHandshake(recStr))
                {
                    string res = GetWebSocketResponse(recStr);

                    theCon.Send(res);
                }
                else
                {
                    int opcode = new string(bytes[0].ToBinString().Copy(4, 4).ToArray()).ToInt_FromBinString();

                    //为关闭连接
                    if (opcode == 8)
                    {
                        GetConnection(theCon.ConnectionId).Close();
                    }
                    else
                    {
                        string recData = AnalyticData(bytes);
                        HandleRecMsg?.Invoke(this, GetConnection(theCon.ConnectionId), recData);
                    }
                }
            };
        }
 /// <summary>
 /// 关闭与服务器的连接
 /// </summary>
 public void Close()
 {
     try {
         _isRec = false;
         _socket.Disconnect(false);
         HandleClientClose?.Invoke(this);
     } catch (Exception ex) {
         HandleException?.Invoke(ex);
     }
 }
Пример #3
0
 /// <summary>
 /// Timer类执行定时到点事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
 {
     _lifetime -= (int)((System.Timers.Timer)(sender)).Interval;
     if (_lifetime <= 0)
     {
         IsActive = false;
         HandleClientClose?.Invoke(this, _server);
         timer.Dispose();
     }
 }
Пример #4
0
 /// <summary>
 /// 删除指定的客户端连接
 /// </summary>
 /// <param name="theConnection">指定的客户端连接</param>
 private void RemoveConnection(SocketConnection theConnection)
 {
     RWLock_ClientList.EnterWriteLock();
     try
     {
         _clientList.Remove(theConnection);
         HandleClientClose?.Invoke(theConnection, this);
     }
     finally
     {
         RWLock_ClientList.ExitWriteLock();
     }
 }
Пример #5
0
        ///// <summary>
        ///// 关闭与服务器的连接
        ///// </summary>
        //public void Close()
        //{
        //    if (_isClosed)
        //        return;

        //    try
        //    {
        //        _isClosed = true;
        //        _isRec = false;
        //        if (IsSocketConnected())
        //        {
        //            _socket.Disconnect(false);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        AccessException(ex);
        //    }
        //    finally
        //    {
        //        try
        //        {
        //            _socket?.Dispose();
        //            HandleClientClose?.Invoke(this);
        //        }
        //        catch (Exception ex)
        //        {
        //            AccessException(ex);
        //        }
        //    }
        //}
        /// <summary>
        /// 关闭与服务器的连接
        /// </summary>
        public void Close()
        {
            if (_isClosed)
            {
                return;
            }
            try
            {
                _isClosed = true;
                _isRec    = false;
                _socket.BeginDisconnect(false, asyncCallback =>
                {
                    try
                    {
                        _socket.EndDisconnect(asyncCallback);
                    }
                    catch (Exception ex)
                    {
                        AccessException(ex);;
                    }
                    finally
                    {
                        _socket.Dispose();
                    }
                }, null);
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                try
                {
                    AccessException(ex);;
                }
                catch
                {
                }
            }
            finally
            {
                try
                {
                    HandleClientClose?.Invoke(this);
                }
                catch (Exception ex)
                {
                    AccessException(ex);;
                }
            }
        }
Пример #6
0
 public void Close()
 {
     try
     {
         _isRec = false;
         _socket.Disconnect(false);
         _server.ClientList.Remove(this);
         HandleClientClose?.Invoke(this, _server);
         _socket.Close();
         _socket.Dispose();
         GC.Collect();
     }
     catch (Exception ex)
     {
         HandleException?.Invoke(ex);
     }
 }
Пример #7
0
 /// <summary>
 /// 关闭与服务器的连接
 /// </summary>
 public void Close()
 {
     try
     {
         _isRec = false;
         _socket.Disconnect(false);
         HandleClientClose?.BeginInvoke(this, null, null);
     }
     catch (Exception ex)
     {
         HandleException?.BeginInvoke(ex, null, null);
     }
     finally
     {
         _socket.Dispose();
         GC.Collect();
     }
 }
        /// <summary>
        /// 关闭当前连接
        /// </summary>
        public void Close()
        {
            if (_isClosed)
            {
                return;
            }
            try
            {
                _isClosed = true;
                _server.RemoveConnection(this);

                _isRec = false;
                _socket.BeginDisconnect(false, (asyncCallback) =>
                {
                    try
                    {
                        _socket.EndDisconnect(asyncCallback);
                    }
                    catch (Exception ex)
                    {
                        HandleException?.Invoke(ex);
                    }
                    finally
                    {
                        _socket.Dispose();
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.Invoke(ex);
            }
            finally
            {
                try
                {
                    HandleClientClose?.Invoke(_server, this);
                }
                catch (Exception ex)
                {
                    HandleException?.Invoke(ex);
                }
            }
        }
 /// <summary>
 /// 关闭与服务器的连接
 /// </summary>
 public void Close()
 {
     try
     {
         _isRec = false;
         _socket.BeginDisconnect(false, asyncCallback =>
         {
             try
             {
                 _socket.EndDisconnect(asyncCallback);
             }
             catch (Exception ex)
             {
                 HandleException?.BeginInvoke(ex, null, null);
             }
             finally
             {
                 _socket.Dispose();
             }
         }, null);
     }
     catch (Exception ex)
     {
         HandleException?.BeginInvoke(ex, null, null);
     }
     finally
     {
         try
         {
             HandleClientClose?.Invoke(this);
         }
         catch (Exception ex)
         {
             HandleException?.Invoke(ex);
         }
     }
 }
Пример #10
0
 /// <summary>
 /// 关闭当前连接
 /// </summary>
 public void Close()
 {
     _theCon.Close();
     _webSocketServer.RemoveConnection(this);
     HandleClientClose?.Invoke(_webSocketServer, this);
 }