Exemplo n.º 1
0
        private void PerformStartupClientCommands(WebSocket sock, BotMessage firstIdnRequest, bool reconnect)
        {
            if (firstIdnRequest == null)
            {
                return;
            }

            Console.WriteLine("connecting...");
            sock.Connect();

            if (!sock.IsAlive)
            {
                return;
            }

            sock.Send(firstIdnRequest.PrintedCommand());

            LoginTime = DateTime.UtcNow;

            //required to wait for the server to stop sending startup messages, otherwise overflow occurs and ws closes
            Console.WriteLine("waiting for " + InitialWaitTime + "ms...");
            Thread.Sleep(InitialWaitTime);

            JoinStartingChannels();

            Thread.Sleep(1000);

            string botStatus = BotOnlineStatus;

            if (_testVersion)
            {
                botStatus = BotTestingStatus;
            }

            SetStatus(STAStatus.Online, botStatus);
        }
Exemplo n.º 2
0
        public void RunLoop()
        {
            if (_debug)
            {
                Console.WriteLine("Runloop started");
            }

            Uptime      = new TimeSpan();
            LastCheckin = new TimeSpan();

            int LastMessageSent = 0;

            using (var ws = new WebSocket(FListChatUri))
            {
                BotMessage firstIdnRequest = GetIdentificationTicket();

                //ws.OnOpen += (sender, e) =>
                //ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.None;

                ws.OnMessage += (sender, e) =>
                                OnMessage(e.Data);

                ws.OnError += (sender, e) =>
                {
                    Console.WriteLine("Websocket Error: " + e.Message);
                    Utils.AddToLog("Websocket Error: " + e.Message, null);
                };

                ws.OnClose += (sender, e) =>
                {
                    Console.WriteLine("WebSocket Close ({0}) ({1})", e.Code, e.Reason);
                    Utils.AddToLog(string.Format("WebSocket Close ({0}) ({1})", e.Code, e.Reason), null);

                    var sslProtocolHack = (System.Security.Authentication.SslProtocols)(SslProtocolsHack.Tls12 | SslProtocolsHack.Tls11 | SslProtocolsHack.Tls);
                    //TlsHandshakeFailure
                    if (e.Code == 1015 && ws.SslConfiguration.EnabledSslProtocols != sslProtocolHack)
                    {
                        Console.WriteLine("activating ssl protocol change");
                        //Utils.AddToLog("activating ssl protocol change", null);
                        ws.SslConfiguration.EnabledSslProtocols = sslProtocolHack;
                        Thread.Sleep(1000);
                        ws.Connect();
                    }
                };


                ws.Origin = "http://localhost:4649";

                ws.EnableRedirection = true;

                PerformStartupClientCommands(ws, firstIdnRequest, false);

                while (true)
                {
                    if (ws.IsAlive)
                    {
                        LastMessageSent += TickTimeMiliseconds;

                        if (MessageQueue.HasMessages())
                        {
                            BotMessage nextMessagePeek = MessageQueue.Peek();

                            if (!Utils.BotMessageIsChatMessage(nextMessagePeek) || LastMessageSent >= MinimumTimeBetweenMessages)
                            {
                                BotMessage nextMessageToSend = MessageQueue.GetNextMessage();

                                if (nextMessageToSend != null)
                                {
                                    if (nextMessageToSend.messageType == "PIN")
                                    {
                                        PinMessageSent++;
                                        if (PinMessageSent % 5 == 0)
                                        {
                                            Console.WriteLine("sending: " + nextMessageToSend.PrintedCommand() + " (#" + PinMessageSent + ")");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("sending: " + nextMessageToSend.PrintedCommand());
                                        Utils.AddToLog("sending: " + nextMessageToSend.PrintedCommand(), nextMessageToSend);
                                    }

                                    ws.Send(nextMessageToSend.PrintedCommand());
                                }
                                if (Utils.BotMessageIsChatMessage(nextMessageToSend))
                                {
                                    LastMessageSent = 0;
                                }
                            }
                        }
                        if (WaitingChannelOpRequests.Count > 0)
                        {
                            if (_testVersion)
                            {
                                Console.WriteLine("Channeloprequest found in queue");
                            }

                            List <UserGeneratedCommand> finished = new List <UserGeneratedCommand>();
                            foreach (UserGeneratedCommand req in WaitingChannelOpRequests)
                            {
                                if (req.ops != null)
                                {
                                    BotCommandController.RunChatBotCommand(req);
                                    finished.Add(req);
                                }
                            }
                            if (finished.Count > 0)
                            {
                                foreach (UserGeneratedCommand rFin in finished)
                                {
                                    WaitingChannelOpRequests.Remove(rFin);
                                }
                            }
                        }

                        Uptime.Add(new TimeSpan(0, 0, 0, 0, TickTimeMiliseconds));

                        PerformCheckinIfNecessary(ref Uptime, ref LastCheckin);
                    }
                    else
                    {
                        if (ReconnectTimer == 0)
                        {
                            Console.WriteLine("Connection was lost.");
                            Console.WriteLine("Waiting for " + (ReconnectTimeMs / 1000) + " seconds to reconnect.");
                            Utils.AddToLog("Waiting for " + (ReconnectTimeMs / 1000) + " seconds to reconnect.", null);
                        }

                        ReconnectTimer += TickTimeMiliseconds;

                        if (ReconnectTimer > ReconnectTimeMs)
                        {
                            BotMessage reconnectIdnRequest = GetIdentificationTicket();
                            PerformStartupClientCommands(ws, reconnectIdnRequest, true);
                            ReconnectTimer = 0;
                        }
                    }
                    Thread.Sleep(TickTimeMiliseconds);
                }
            }
        }