Пример #1
0
        public async Task <bool> Connect(Uri uri, bool ignoreReceiver = false)
        {
            try
            {
                ConnectionClosed = false;
                _commandCount    = 0;
                _connection      = new MessageWebSocket();
                _connection.Control.MessageType = SocketMessageType.Utf8;
                //  if(ignoreReceiver==false)
                _connection.MessageReceived += Connection_MessageReceived;

                _connection.Closed += Connection_Closed;
                await _connection.ConnectAsync(uri);

                IsConnected?.Invoke(true);

                _messageWriter = new DataWriter(_connection.OutputStream);
                return(true);
            }
            catch (Exception e)
            {
                switch (SocketError.GetStatus(e.HResult))
                {
                case SocketErrorStatus.HostNotFound:
                    // Handle HostNotFound Error
                    break;

                default:
                    // Handle Unknown Error
                    break;
                }
                return(false);
            }
        }
Пример #2
0
 /// <summary> Returns true if the vessel has any connection</summary>
 public static bool Connected(Guid id)
 {
     return(RT_API != null && (bool)IsConnected.Invoke(null, new Object[] { id }));
 }
Пример #3
0
        public async Task <bool> Connect(Uri uri, bool ignoreReceiver = false)
        {
            try
            {
                ConnectionClosed = false;
                _commandCount    = 0;
                _connection      = new MessageWebSocket();
                _connection.Control.MessageType = SocketMessageType.Utf8;
                _connection.MessageReceived    += Connection_MessageReceived;
                _connection.Closed += Connection_Closed;

                try
                {
                    await _connection.ConnectAsync(uri).AsTask().WaitAsync(TimeSpan.FromSeconds(5));

                    _messageWriter = new DataWriter(_connection.OutputStream);

                    IsConnected?.Invoke(true);
                    return(true);
                }
                catch (TimeoutException)
                {
                    return(false);
                }

                //using (var cancellationTokenSource = new CancellationTokenSource(5000))
                //{
                //    var connectTask = _connection.ConnectAsync(uri).AsTask(cancellationTokenSource.Token);
                //    var result = await connectTask.ContinueWith((antecedent) =>
                //    {
                //        if (antecedent.Status == TaskStatus.RanToCompletion)
                //        {
                //            // connectTask ran to completion, so we know that the MessageWebSocket is connected.
                //            // Add additional code here to use the MessageWebSocket.
                //            IsConnected?.Invoke(true);

                //            _messageWriter = new DataWriter(_connection.OutputStream);
                //            return true;
                //        }
                //        else
                //        {
                //            // connectTask timed out, or faulted.
                //            return false;
                //        }
                //    });

                //    return result;
                //}
            }
            catch (Exception e)
            {
                switch (SocketError.GetStatus(e.HResult))
                {
                case SocketErrorStatus.HostNotFound:
                    // Handle HostNotFound Error
                    break;

                default:
                    // Handle Unknown Error
                    break;
                }
                return(false);
            }
        }