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); } }
/// <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 })); }
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); } }