private void CloseSocket(CombinedWebSocket sock) { lock (ActiveWebSockets) { if (ActiveWebSockets.Contains(sock)) { ActiveWebSockets.Remove(sock); try { _ = sock.CloseAsync(); } catch { } } } }
private async Task OpenWebSocketAsync(SockStream[] streamsPerSocket) { string endpoint = this.CombinedWebsocketUri; foreach (var str in streamsPerSocket) { endpoint += str.SockName + "/"; } endpoint = endpoint.Remove(endpoint.Length - 1); var websocket = new CombinedWebSocket { Streams = streamsPerSocket }; void onMsg(WebSocketWrapper sender, string msg) { try { var datum = JsonConvert.DeserializeObject <BinanceCombinedWebsocketData>(msg); SockStream stream; bool found; lock (Streams) found = Streams.TryGetValue(datum.StreamName, out stream); if (found) { stream.Pulse(datum.RawData); } else { Console.WriteLine("sockNotFound"); } websocket.WatchDog.Restart(); } catch { } } await websocket.ConnectAsync(endpoint, onMsg); websocket.WatchDog.Restart(); lock (ActiveWebSockets) ActiveWebSockets.Add(websocket); }