public async Task Connect(string url, string username, string password, CancellationToken token) { try { m_Time = DateTime.UtcNow; var protocols = new List <SupportedProtocol>(); SupportedProtocol p; p = EtpHelper.ToSupportedProtocol(Protocols.ChannelStreaming, "consumer"); protocols.Add(p); p = EtpHelper.ToSupportedProtocol(Protocols.Discovery, "store"); protocols.Add(p); var auth = Authorization.Basic(username, password); m_client = EtpFactory.CreateClient(WebSocketType.WebSocket4Net, url, m_ApplicationName, m_ApplicationVersion, SUBPROTOCOL, auth); m_client.Register <IChannelStreamingProducer, ChannelStreamingProducerHandler>(); m_client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); m_client.Register <IStoreCustomer, StoreCustomerHandler>(); m_client.Handler <ICoreClient>().OnOpenSession += HandleOpenSession; CreateSession(protocols); await m_client.OpenAsync(); } catch (Exception ex) { if (ex.InnerException != null) { throw new Exception($"{ex.Message} {ex.InnerException.Message}"); } throw; } }
public async Task Connect(string url, string username, string password, List <SupportedProtocol> protocols, CancellationToken token) { try { var auth = Authorization.Basic(username, password); m_client = EtpFactory.CreateClient(WebSocketType.WebSocket4Net, url, "ShellSquare ETP Client", "1.4.1.1", SUBPROTOCOL, auth); var protocol1Info = protocols.Where(x => x.Protocol == 1).FirstOrDefault(); if (protocol1Info.Role.Equals("producer")) { m_client.Register <IChannelStreamingConsumer, ChannelStreamingConsumerHandler>(); m_client.Handler <IChannelStreamingConsumer>().OnChannelMetadata += HandleChannelMetadata; m_client.Handler <IChannelStreamingConsumer>().OnProtocolException += HandleProtocolException; m_client.Handler <IChannelStreamingConsumer>().OnChannelData += HandleChannelData; } else { m_client.Register <IChannelStreamingProducer, ChannelStreamingProducerHandler>(); } m_client.Register <IDiscoveryCustomer, DiscoveryCustomerHandler>(); m_client.Register <IStoreCustomer, StoreCustomerHandler>(); m_client.Handler <ICoreClient>().OnOpenSession += HandleOpenSession; await CreateSession(protocols); await m_client.OpenAsync(); } catch (Exception ex) { if (ex.InnerException != null) { throw new Exception($"{ex.Message} {ex.InnerException.Message}"); } throw; } }
/// <summary> /// Opens a WebSocket connection and waits for the SocketOpened event to be called. /// </summary> /// <param name="client">The client.</param> /// <param name="milliseconds">The timeout, in milliseconds.</param> /// <returns>An awaitable task.</returns> public static async Task <bool> OpenAsyncWithTimeout(this IEtpClient client, int?milliseconds = null) { var onSessionOpened = HandleAsync <SessionOpenedEventArgs>(x => client.SessionOpened += x); var open = client.OpenAsync(); await Task.WhenAll(open, onSessionOpened).WaitAsync(milliseconds); return(client.IsWebSocketOpen); }
/// <summary> /// Opens a WebSocket connection and waits for the SocketOpened event to be called. /// </summary> /// <param name="client">The client.</param> /// <param name="milliseconds">The timeout, in milliseconds.</param> /// <returns>An awaitable task.</returns> public static async Task <bool> OpenAsyncWithTimeout(this IEtpClient client, int?milliseconds = null) { await client.OpenAsync().WaitAsync(milliseconds); return(client.IsOpen); }