예제 #1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            if (e.Data == "Hi Server")
            {
                Console.WriteLine(e.Data);
                Send("Hi Client");
            }
            else
            {
                Blockchainblock newChain = JsonConvert.DeserializeObject <Blockchainblock>(e.Data);

                if (newChain.IsValid() && newChain.Chain.Count > Blockchain.governmentChain.Chain.Count)
                {
                    List <Transaction> newTransactions = new List <Transaction>();
                    // newTransactions.AddRange(newChain.PendingTransactions);
                    // newTransactions.AddRange(Program.GovernmentChain.PendingTransactions);

                    // newChain.PendingTransactions = newTransactions;
                    Blockchain.governmentChain = newChain;
                }

                if (!chainSynched)
                {
                    Send(JsonConvert.SerializeObject(Blockchain.governmentChain));
                    chainSynched = true;
                }
            }
        }
예제 #2
0
        public Boolean 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
                    {
                        Blockchainblock newChain = JsonConvert.DeserializeObject <Blockchainblock>(e.Data);
                        if (newChain.IsValid() && newChain.Chain.Count > Blockchain.governmentChain.Chain.Count)
                        {
                            List <Transaction> newTransactions = new List <Transaction>();
                            // newTransactions.AddRange(newChain.PendingTransactions);
                            // newTransactions.AddRange(Program.GovernmentChain.PendingTransactions);

                            // newChain.PendingTransactions = newTransactions;
                            Blockchain.governmentChain = newChain;
                        }
                    }
                };
                ws.Connect();
                if (ws.IsAlive)
                {
                    ws.Send("Hi Server");
                    ws.Send(JsonConvert.SerializeObject(Blockchain.governmentChain));
                    wsDict.Add(url, ws);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }