示例#1
0
        public void Login(string url, string deviceId, string apiKey)
        {
            if (_isLoggedIn)
            {
                throw new InvalidOperationException("Already logged in");
            }

            _webSocketConnection.Connect(url);
            _webSocketConnection.OnMessage(MessagePayloadReceived);

            if (_webSocketConnection.IsDisconnected)
            {
                throw new DisconnectedException();
            }

            _webSocketConnection.Send($"login {deviceId} {apiKey}");

            if (_responseEvent.WaitOne(TimeSpan.FromSeconds(10)))
            {
                if (_lastResponse == "login ack")
                {
                    _isLoggedIn = true;
                }
                else
                {
                    throw new LoginInvalidException(_lastResponse);
                }
            }
            else
            {
                throw new TimeoutException("Login timeout expired");
            }
        }