protected override void OnMessage(MessageEventArgs e) { if (e.Data == "Hi Server") { Console.WriteLine(e.Data); Send("Hi Client"); } else { BlockChain newChain = JsonConvert.DeserializeObject <BlockChain>(e.Data); if (newChain != null) { Console.WriteLine("Receive the newChain"); } if (_blockchain != null) { Console.WriteLine("Receive the _blockchain"); } if (newChain.IsValid() && newChain.Count > _blockchain.Count) { List <Transaction> newTransactions = new List <Transaction>(); newTransactions.AddRange(newChain.PendingTransactions); newTransactions.AddRange(_blockchain.PendingTransactions); newChain.PendingTransactions = newTransactions; _blockchain = newChain; } if (!chainSynched) { Send(JsonConvert.SerializeObject(_blockchain)); chainSynched = true; } } }
public void Connect(string url) { if (!wsDict.ContainsKey(url)) { WebSocket ws = new WebSocket(url); ws.OnMessage += (sender, e) => { if (e.Data == "Hi Client") { Console.WriteLine(e.Data); } else { BlockChain newChain = JsonConvert.DeserializeObject <BlockChain>(e.Data); if (newChain.IsValid() && newChain.Count > _blockchain.Count) { List <Transaction> newTransactions = new List <Transaction>(); newTransactions.AddRange(newChain.PendingTransactions); newTransactions.AddRange(_blockchain.PendingTransactions); newChain.PendingTransactions = newTransactions; _blockchain = newChain; } } }; ws.Connect(); ws.Send("Hi Server"); ws.Send(JsonConvert.SerializeObject(_blockchain)); wsDict.Add(url, ws); } }