Ejemplo n.º 1
0
 public override bool Equals(object aObj)
 {
     if (aObj is WebSocketSubProtocol)
     {
         WebSocketSubProtocol lOther = (WebSocketSubProtocol)aObj;
         return(mSubProtocol.Equals(lOther.mSubProtocol) && mEncoding.Equals(lOther.mEncoding));
     }
     else
     {
         return(base.Equals(aObj));
     }
 }
Ejemplo n.º 2
0
 private void AddSubProtocol(WebSocketSubProtocol aSubProtocol)
 {
     if (mSubProtocols == null)
         mSubProtocols = new List<WebSocketSubProtocol>();
     else
         mSubProtocols.Add(aSubProtocol);
 }
Ejemplo n.º 3
0
        public void Open(string aURI, string aSubProtocol, int aTimeout, int aVersion)
        {
            try
            {
                if (mLog.IsDebugEnabled)
                    mLog.Debug(WebSocketMessage.ESTABLiSHING_CONNECTION + WebSocketMessage.SEPARATOR
                        + WebSocketMessage.URL + aURI + WebSocketMessage.SEPARATOR + WebSocketMessage.SUBPROTOCOL
                        + aSubProtocol + WebSocketMessage.SEPARATOR + WebSocketMessage.TIMEOUT
                        + aTimeout + WebSocketMessage.SEPARATOR + WebSocketMessage.VERSION + aVersion);

                mStatus = WebSocketStatus.OPENING;

                mVersion = aVersion;
                mURI = new Uri(aURI);

                WebSocketHandshake lHandshake = new WebSocketHandshake(mURI, aSubProtocol, mVersion,mCookieManage);

                if (mSocket != null && mSocket.Connected)
                    mSocket.Close();
                 CreateSocket();

                byte[] sendBuffer = lHandshake.GenerateC2SRequest();
                mNetStream.Write(sendBuffer, 0, sendBuffer.Length);
                mNetStream.Flush();

                mStatus = WebSocketStatus.CONNECTING;

                mHeaders.ReadRequestFromBuffer(sendBuffer);
                if (mLog.IsDebugEnabled)
                    mLog.Debug(WebSocketMessage.SENDING_HANDSHAKE + mHeaders.ToStringRequest());

                WebSocketTimeout.CallWithTimeout(mHeaders.ReadResponseFromStream, aTimeout, mNetStream);

                mCookieManage.AddCookies(mHeaders.GetCookies, mURI);

                if (mLog.IsDebugEnabled)
                    mLog.Debug(WebSocketMessage.RECEIVING_HANDSHAKE + mHeaders.ToStringResponse());

                lHandshake.VerifyS2CResponse(mHeaders);

                string lProtocol = GetResponseHeaderField(WebSocketConstants.SEC_WEBSOCKET_PROTOCOL);

                if (lProtocol != null)
                    mNegotiatedSubProtocol = new WebSocketSubProtocol(lProtocol, mEncoding);
                else
                    mNegotiatedSubProtocol = new WebSocketSubProtocol(WebSocketConstants.WS_SUBPROT_DEFAULT,
                        WebSocketConstants.WS_ENCODING_DEFAULT);

                Thread lReciver = new Thread(new ThreadStart(Receiver));
                lReciver.Start();

                if (mLog.IsInfoEnabled)
                    mLog.Info(WebSocketMessage.CONNECTION_HAS_BEEN_ESTABLISHED);

                mStatus = WebSocketStatus.OPENED;
                mIsRunning = true;
                OnOpenConnection(mHeaders);
            }
            catch (TimeoutException lTe)
            {
                if (mLog.IsErrorEnabled)
                    mLog.Error(WebSocketMessage.EXCEEDED_FOR_CONNECTION + WebSocketMessage.SEPARATOR
                        + WebSocketMessage.DETAILS + lTe.Message);
                mCLose = WebSocketCloseReason.TIMEOUT;
                OnCloseConnection(mCLose);
                OnError(new WebSocketError(WebSocketMessage.TIMEOUT + WebSocketMessage.EXCEEDED_FOR_CONNECTION, mCLose));
                CheckReconnect();
            }
            catch (Exception lEx)
            {
                if (mLog.IsErrorEnabled)
                    mLog.Error(WebSocketMessage.NOT_ESTABLISH_CONNECTION + WebSocketMessage.SEPARATOR
                        + WebSocketMessage.DETAILS + lEx.Message);
                mCLose = WebSocketCloseReason.BROKEN;
                OnCloseConnection(mCLose);
                OnError(new WebSocketError(WebSocketMessage.NOT_ESTABLISH_CONNECTION, mCLose));
                CheckReconnect();
            }
        }