Beispiel #1
0
 public WebSocketClient(string uri, string userName, string password, string applianceGuid, string applianceDisplayName)
 {
     //ServicePointManager.ServerCertificateValidationCallback = OnValidationCallback;
     _waitConnectionOpenedEvent = new AutoResetEvent(false);
     Status = WebSessionStatus.Inactive;
     _uri = uri;
     _userName = userName;
     _password = password;
     _applianceGuid = applianceGuid;
     _applianceDisplayName = applianceDisplayName;
     Login();
 }
Beispiel #2
0
        public void Login()
        {
            try
            {
                if (Status != WebSessionStatus.Active)
                {
                    Console.WriteLine("Try to login webscoket server now");
                    _waitConnectionOpenedEvent.Reset();
                    Status = WebSessionStatus.Logging;
                    Interlocked.Increment(ref _loginAttemps);
                    var cookies = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>(WebSocketConst.UserNameKey, _userName),
                        new KeyValuePair<string, string>(WebSocketConst.PasswordKey, _password),
                        new KeyValuePair<string, string>(WebSocketConst.ClientGuidKey, _applianceGuid),
                        new KeyValuePair<string, string>(WebSocketConst.ClientHostNameKey, _applianceDisplayName),
                        new KeyValuePair<string, string>(WebSocketConst.WebSocketClientVersion, "2")};

                    _webSocket = new WebSocket(_uri, cookies: cookies)
                    {
                        AllowUnstrustedCertificate = false,
                        EnableAutoSendPing = false
                    };
                    _webSocket.Closed += WebSocket_Closed;
                    _webSocket.Error += WebSocket_Error;
                    _webSocket.MessageReceived += webSocket_MessageReceived;
                    _webSocket.DataReceived += websocket_DataReceived;
                    _webSocket.Opened += WebSocket_Opened;
                    _webSocket.Open();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Beispiel #3
0
 public void Dispose()
 {
     try
     {
         _webSocket.Close();
         Status = WebSessionStatus.Inactive;
     }
     catch
     {
         //Ignore this exception because we don't care about it.
     }
 }
Beispiel #4
0
 private void WebSocket_Closed(object sender, EventArgs e)
 {
     Status = WebSessionStatus.Inactive;
 }
Beispiel #5
0
 private void WebSocket_Opened(object sender, EventArgs e)
 {
     Status = WebSessionStatus.Active;
 }
Beispiel #6
0
        private void webSocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            //var response = JsonConvert.DeserializeObject<WebSocketResponse>(e.Message);
            //if (response.ResponseType == WebSocketResponseType.PutFileResponse)
            //{
            //    Interlocked.Increment(ref _getPutResponseCnt);
            //}
            //switch (response.ResponseType)
            //{
            //    case WebSocketResponseType.AuthenticationResponse:
            //        {
            //            if (response.ExecuteSuccess)
            //            {
            //                Status = WebSessionStatus.Active;
            //                Console.WriteLine("WebSocket Connection is opened" + Environment.NewLine);
            //            }
            //            else
            //            {
            //                Status = WebSessionStatus.Inactive;
            //                Interlocked.Increment(ref _loginAttemps);
            //                Console.WriteLine("Exception during authentication: " + response.ExceptionItem);
            //            }
            //            _waitConnectionOpenedEvent.Set();
            //            break;
            //        }
            //    case WebSocketResponseType.PutFileResponse:
            //        {
            //            var putFileResponse = JsonConvert.DeserializeObject<PutFileResponse>(e.Message);
            //            WebSocketNotificationHandler.ReceiveResponse(response.RequestId, putFileResponse.ExecuteSuccess);
            //            break;
            //        }
            //    default:
            //        Console.WriteLine("Unrecognized WebSocket response");
            //        break;
            //}

            WebSocketResponseType responseType;

            string[] responseBody = e.Message.Split(';');
            //responseType = responseBody[0].Split(':')[1];
            Enum.TryParse(responseBody[0].Split(':')[1], out responseType);
            var requestId = long.Parse(responseBody[1].Split(':')[1]);
            var executeResult = bool.Parse(responseBody[2].Split(':')[1]);
            

            if (responseType == WebSocketResponseType.PutFileResponse)
            {
                Interlocked.Increment(ref _getPutResponseCnt);
            }
            switch (responseType)
            {
                case WebSocketResponseType.AuthenticationResponse:
                    {
                        if (executeResult)
                        {
                            Status = WebSessionStatus.Active;
                            Console.WriteLine("WebSocket Connection is opened" + Environment.NewLine);
                        }
                        else
                        {
                            Status = WebSessionStatus.Inactive;
                            Interlocked.Increment(ref _loginAttemps);
                            Console.WriteLine("Exception during authentication: ");
                        }
                        _waitConnectionOpenedEvent.Set();
                        break;
                    }
                case WebSocketResponseType.PutFileResponse:
                    {
                        //var putFileResponse = JsonConvert.DeserializeObject<PutFileResponse>(e.Message);
                        //WebSocketNotificationHandler.ReceiveResponse(requestId, putFileResponse.ExecuteSuccess);
                        WebSocketNotificationHandler.ReceiveResponse(requestId, executeResult);
                        break;
                    }
                default:
                    Console.WriteLine("Unrecognized WebSocket response");
                    break;
            }
        }
Beispiel #7
0
        private void websocket_DataReceived(object sender, DataReceivedEventArgs e)
        {
            var response = WebSocketBlockWrite.BytesToResponse(e.Data);

            if (response.ResponseType == WebSocketResponseType.PutFileResponse)
            {
                Interlocked.Increment(ref _getPutResponseCnt);
            }
            switch (response.ResponseType)
            {
                case WebSocketResponseType.AuthenticationResponse:
                    {
                        if (response.ExecuteSuccess)
                        {
                            Status = WebSessionStatus.Active;
                            Console.WriteLine("WebSocket Connection is opened" + Environment.NewLine);
                        }
                        else
                        {
                            Status = WebSessionStatus.Inactive;
                            Interlocked.Increment(ref _loginAttemps);
                            Console.WriteLine("Exception during authentication: ");
                        }
                        _waitConnectionOpenedEvent.Set();
                        break;
                    }
                case WebSocketResponseType.PutFileResponse:
                    {
                        //var putFileResponse = JsonConvert.DeserializeObject<PutFileResponse>(e.Message);
                        //WebSocketNotificationHandler.ReceiveResponse(requestId, putFileResponse.ExecuteSuccess);
                        WebSocketNotificationHandler.ReceiveResponse(response.RequestId, response.ExecuteSuccess);
                        break;
                    }
                default:
                    Console.WriteLine("Unrecognized WebSocket response");
                    break;
            }
        }