/// <summary> /// Connect a web socket to a path on the API and start listening, not all exchanges support this /// </summary> /// <param name="url">The sub url for the web socket, or null for none</param> /// <param name="messageCallback">Callback for messages</param> /// <param name="connectCallback">Connect callback</param> /// <returns>Web socket - dispose of the wrapper to shutdown the socket</returns> public IWebSocket ConnectWebSocket ( string url, Func <IWebSocket, byte[], Task> messageCallback, WebSocketConnectionDelegate connectCallback = null, WebSocketConnectionDelegate disconnectCallback = null ) { if (messageCallback == null) { throw new ArgumentNullException(nameof(messageCallback)); } string fullUrl = BaseUrlWebSocket + (url ?? string.Empty); ExchangeSharp.ClientWebSocket wrapper = new ExchangeSharp.ClientWebSocket { Uri = new Uri(fullUrl), OnBinaryMessage = messageCallback }; if (connectCallback != null) { wrapper.Connected += connectCallback; } if (disconnectCallback != null) { wrapper.Disconnected += disconnectCallback; } wrapper.Start(); return(wrapper); }
public Task <IWebSocket> ConnectWebSocketAsync ( string url, Func <IWebSocket, byte[], Task> messageCallback, WebSocketConnectionDelegate?connectCallback = null, WebSocketConnectionDelegate?disconnectCallback = null ) { if (messageCallback == null) { throw new ArgumentNullException(nameof(messageCallback)); } ExchangeSharp.ClientWebSocket wrapper = new ExchangeSharp.ClientWebSocket { Uri = new Uri(url), OnBinaryMessage = messageCallback }; if (connectCallback != null) { wrapper.Connected += connectCallback; } if (disconnectCallback != null) { wrapper.Disconnected += disconnectCallback; } wrapper.Start(); return(Task.FromResult <IWebSocket>(wrapper)); }
/// <summary> /// Connect a web socket to a path on the API and start listening, not all exchanges support this /// </summary> /// <param name="url">The sub url for the web socket, or null for none</param> /// <param name="messageCallback">Callback for messages</param> /// <param name="connectCallback">Connect callback</param> /// <returns>Web socket - dispose of the wrapper to shutdown the socket</returns> public IWebSocket ConnectWebSocket ( string url, Func <IWebSocket, byte[], Task> messageCallback, WebSocketConnectionDelegate connectCallback = null, WebSocketConnectionDelegate disconnectCallback = null ) { string fullUrl = BaseUrlWebSocket + (url ?? string.Empty); ExchangeSharp.ClientWebSocket wrapper = new ExchangeSharp.ClientWebSocket { Uri = new Uri(fullUrl), OnMessage = messageCallback, KeepAlive = TimeSpan.FromSeconds(5.0) }; if (connectCallback != null) { wrapper.Connected += connectCallback; } if (disconnectCallback != null) { wrapper.Disconnected += disconnectCallback; } wrapper.Start(); return(wrapper); }