示例#1
0
 private void AddSubProtocol(WebSocketSubProtocol aSubProtocol)
 {
     if (mSubProtocols == null)
     {
         mSubProtocols = new List <WebSocketSubProtocol>();
     }
     else
     {
         mSubProtocols.Add(aSubProtocol);
     }
 }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebSocketTokenClient"/> class.
 /// </summary>
 public WebSocketTokenClient()
 {
     SubProtocol = new WebSocketSubProtocol(WebSocketConstants.WS_SUBPROT_JSON, WebSocketEncoding.TEXT);
     _tokenProcessor = new JSONTokenProcessor();
 }
示例#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();
            }
        }
示例#4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebSocketTokenClient"/> class.
 /// </summary>
 public WebSocketTokenClient()
 {
     SubProtocol     = new WebSocketSubProtocol(WebSocketConstants.WS_SUBPROT_JSON, WebSocketEncoding.TEXT);
     _tokenProcessor = new JSONTokenProcessor();
 }