protected virtual void OnInterpreterProcessedConnected(BitMexWebsocketConnected c) { if (InterpreterProcessedConnected != null) { InterpreterProcessedConnected(this, c); } }
//When we try to connect to the websocket the objects are instantiated private void btnConnect_Click(object sender, EventArgs e) { try { if (btnConnect.Text == "Connect") { if (cboWebsocketURL.SelectedIndex != 1 && cboWebsocketURL.SelectedIndex != 0) { return; } string apiKey = ""; string apiSecret = ""; if (websocketClient == null || !websocketClient.GetIsConnected()) { if (chkAuthenticate.Checked) { apiKey = txtAPIKey.Text; apiSecret = txtAPISecret.Text; } websocketClient = new BitMexWebsocketClient(new Uri(cboWebsocketURL.Text), apiKey, apiSecret); websocketInterpreter = new BitMexWebsocketInterpreter(); connected = new BitMexWebsocketConnected(); DelegateManagement(); } websocketClient.Connect(); } if (btnConnect.Text == "Disconnect") { websocketClient.Disconnect(); websocketClient = null; } } catch (Exception ex) { ActivityLog.Error("FORM", ex.Message); } }
//ProcessMessage is called every time a message is received from the Websocket, here the message is interpreted and directed to the correct function public void ProcessMessage(object source, string message) { //The Websocket message contains an update regarding the CONNECTED subscription //We get rid of the unnecessary part of the string and deserialise the remaining to obtain an object of BitMexWebsocketConnected type //We then pass the call another event to update the object through a Delegate if (message.Contains("\"table\":\"connected\"")) { try { string search = "\"data\":"; int i = message.IndexOf(search); string json = message.Substring(i + search.Length); json = json.Remove(json.Length - 1); BitMexWebsocketConnected connected = JsonConvert.DeserializeObject <List <BitMexWebsocketConnected> >(json).First(); OnInterpreterProcessedConnected(connected); return; } catch (Exception e) { ActivityLog.Error("WEBSOCKET INTERPRETER", e.Message); } } //We add this check so that we do not log API Keys in plain text in the log file if (message.Contains("error") && !message.Contains("authKey")) { ActivityLog.Error("WEBSOCKET INTERPRETER", message); return; } //We add this check so that we do not log API Keys in plain text in the log file if (message.Contains("error") && message.Contains("authKey")) { ActivityLog.Error("WEBSOCKET INTERPRETER", "Authentication Failed, Please Check API Keys"); return; } }
//Update the values of the Textboxes when the object is updated public void UpdateConnectedValues(object source, BitMexWebsocketConnected c) { txtConnectedBots.Text = connected.Bots; txtConnectedUsers.Text = connected.Users; }