コード例 #1
0
        private void AsyncCallback(IAsyncResult result)
        {
            // socket 连接失败
            string error = this._asyncDel.EndInvoke(result).Trim();

            if (string.IsNullOrEmpty(error) == false)
            {
                this._isConnected = false;
                if (this.OnException != null)
                {
                    this.OnException(error);
                }

                return;
            }

            // 启动协议,开始握手
            StartParamType type = (StartParamType)result.AsyncState;

            try
            {
                switch (type)
                {
                case StartParamType.NONE:
                    protocol.start(null, null);
                    break;

                case StartParamType.JSON:
                    protocol.start(this._user, null);
                    break;

                case StartParamType.HAND_SHAKE_CALLBACK:
                    protocol.start(null, this._handshakeCallback);
                    break;

                case StartParamType.ALL:
                    protocol.start(this._user, this._handshakeCallback);
                    break;
                }

                this._isConnected = true;
                if (this.OnConnect != null)
                {
                    this.OnConnect();
                }
            }
            catch (Exception e)
            {
                this._isConnected = false;
                if (this.OnException != null)
                {
                    this.OnException(e.Message);
                }
            }
        }
コード例 #2
0
 private void InitClient(StartParamType type)
 {
     this._asyncDel = this.AsyncInit;
     IAsyncResult ret = this._asyncDel.BeginInvoke(this.AsyncCallback, type);
 }