Ejemplo n.º 1
0
        private void Connect()
        {
            var servers = JsonUtil.DeserializeUrl <HitboxServer[]>("https://www.hitbox.tv/api/chat/servers?redis=true");

            if (servers == null)
            {
                return;
            }

            int port = 0;

            if (int.TryParse(webSocket.Port, out port))
            {
                GetRandomIP(servers.Select(s => s.server_ip).ToArray(), int.Parse(webSocket.Port), (ip) =>
                {
                    if (!String.IsNullOrWhiteSpace(ip))
                    {
                        using (var webClient = new WebClientBase())
                        {
                            string pathHash = this.With(x => webClient.Download(String.Format("http://{0}/socket.io/1/{1}", ip, Time.UnixTimestamp())))
                                              .With(x => new String(x.TakeWhile(c => !c.Equals(':')).ToArray()));

                            webSocket.Path = String.Format("/socket.io/1/websocket/{0}", pathHash);
                            webSocket.Host = ip;
                            webSocket.Connect();
                        }
                    }
                });
            }
        }
Ejemplo n.º 2
0
        public void InitWebClient()
        {
            Method          = "GET";
            TimeoutMs       = 60000;
            Interval        = 30000;
            Delay           = 0;
            wc              = new WebClientBase();
            Cookies         = wc.Cookies;
            IsLongPoll      = false;
            IsTimeStamped   = true;
            IsAnonymous     = false;
            TimeoutOnError  = true;
            wc.Timeout      = TimeoutMs;
            wc.ErrorHandler = (error) =>
            {
                Log.WriteError(error);
                if (gotError && TimeoutOnError)
                {
                    timer.Change(60000, 60000);
                    return;
                }

                gotError = true;
            };
            wc.SuccessHandler = () =>
            {
                if (gotError)
                {
                    timer.Change(Interval, Interval);
                }

                gotError = false;
            };
        }
Ejemplo n.º 3
0
 private void InitWebClient()
 {
     webClient              = new WebClientBase();
     webClient.Timeout      = 30000;
     webClient.KeepAlive    = false;
     webClient.IsAnonymous  = true;
     webClient.ErrorHandler = (error) => {
         InitWebClient();
     };
 }
Ejemplo n.º 4
0
        public ConnectcastChat(ChatConfig config)
            : base(config)
        {
            ReceiveOwnMessages = false;

            EmoticonFallbackUrl = @"Content\dummy.html";
            EmoticonUrl         = "http://dummy.com";

            CreateChannel  = () => { return(new ConnectcastChannel(this)); };
            LoginWebClient = new WebClientBase();

            //ContentParsers.Add(MessageParser.ParseURLs);
            //ContentParsers.Add(MessageParser.ParseEmoticons);
        }
Ejemplo n.º 5
0
        public static void ParseSimpleImageTags(ChatMessage message, IChat chat, bool isInsideTag = true)
        {
            string regex = @"(<((https?:)?\/\/?[^'""<>]+?\.(jpg|jpeg|gif|png)).*?>)";

            if (!isInsideTag)
            {
                regex = @"(((https?:)?\/\/?[^'""<>]+?\.(jpg|jpeg|gif|png)).*)";
            }
            Regex r       = new Regex(regex, RegexOptions.IgnoreCase);
            var   matches = r.Matches(message.Text);

            using (var webClient = new WebClientBase())
            {
                foreach (Match match in matches)
                {
                    if (match.Groups.Count > 1)
                    {
                        message.Text = r.Replace(message.Text, @"<img src=""$2""/>").Replace("href=\"www", "href=\"http://www");
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public override void Join(Action <IChatChannel> callback, string channel)
        {
            ircClient = new IrcClient();

            var safeConnectDelay = Chat.ChatChannels.Count * 100;

            Thread.Sleep(safeConnectDelay);
            ChannelName = "#" + channel.Replace("#", "");

            pingTimer = new Timer((sender) => {
                TryIrc(() => ircClient.Ping());
                (sender as TwitchChannel).disconnectTimer.Change(10000, Timeout.Infinite);
            }, this, Timeout.Infinite, Timeout.Infinite);

            disconnectTimer = new Timer((sender) => {
                Log.WriteError("{0} ping timeout", Chat.ChatName);
                TryIrc(() => Leave());
            }, this, Timeout.Infinite, Timeout.Infinite);

            SetupStatsWatcher();

            using (WebClientBase webClient = new WebClientBase())
            {
                var badgesJson = this.With(x => webClient.Download(String.Format(@"https://api.twitch.tv/kraken/chat/{0}/badges", ChannelName.Replace("#", ""))))
                                 .With(x => JsonConvert.DeserializeObject <TwitchBadges>(x));

                if (badgesJson != null)
                {
                    channelBadges["admin"]       = this.With(x => badgesJson.admin).With(x => x.image);
                    channelBadges["broadcaster"] = this.With(x => badgesJson.broadcaster).With(x => x.image);
                    channelBadges["mod"]         = this.With(x => badgesJson.mod).With(x => x.image);
                    channelBadges["staff"]       = this.With(x => badgesJson.staff).With(x => x.image);
                    channelBadges["turbo"]       = this.With(x => badgesJson.turbo).With(x => x.image);
                    channelBadges["subscriber"]  = this.With(x => badgesJson.subscriber).With(x => x.image);
                }
            }
            SetUserBadge(ChannelName.ToLower().Replace("#", ""), "broadcaster");

            JoinCallback = callback;
            var nickname         = Chat.IsAnonymous ? (Chat as ChatBase).AnonymousNickName : Chat.NickName;
            var registrationInfo = new IrcUserRegistrationInfo()
            {
                UserName = nickname,
                NickName = nickname,
                RealName = nickname,
                Password = Chat.IsAnonymous ? "blah" : "oauth:" + Chat.Config.GetParameterValue("OAuthToken") as string,
            };

            var host     = Chat.Config.GetParameterValue("Host") as string;
            var portText = Chat.Config.GetParameterValue("Port") as string;
            int port     = 6667;

            int.TryParse(portText, out port);

            if (TryIrc(() => {
                ircClient.Initialize();
                ircClient.Disconnected += ircClient_Disconnected;
                ircClient.RawMessageReceived += ircClient_RawMessageReceived;
                ircClient.PongReceived += ircClient_PongReceived;

                if (Regex.IsMatch(host, @"\d+\.\d+\.\d+\.\d+"))
                {
                    ircClient.Connect(host, port, false, registrationInfo);
                }
                else
                {
                    Utils.Net.TestTCPPort(host, port, (hostList, error) =>
                    {
                        if (hostList == null || hostList.AddressList.Count() <= 0)
                        {
                            Log.WriteError("All servers are down. Domain:" + host);
                            Leave();
                        }
                        else
                        {
                            ircClient.Connect(hostList.AddressList[random.Next(0, hostList.AddressList.Count())], port, false, registrationInfo);
                        }
                    });
                }
            }) != null)
            {
                Leave();
            }
        }
Ejemplo n.º 7
0
        public override void Join(Action <IChatChannel> callback, string channel)
        {
            ChannelName = "#" + channel.Replace("#", "");

            if (ChannelName.Contains("@"))
            {
                return;
            }

            string sid = String.Empty;

            using (WebClientBase webClient = new WebClientBase())
            {
                webClient.Cookies = (Chat as ConnectcastChat).LoginWebClient.Cookies;
                channelToken      = this.With(x => webClient.Download(String.Format("http://connectcast.tv/chat/{0}", ChannelName.Replace("#", ""))))
                                    .With(x => Re.GetSubString(x, "token[^']*'(.*?)'"));

                sid = this.With(x => webClient.Download(
                                    String.Format("https://chat.connectcast.tv:3000/socket.io/?EIO=3&transport=polling&t=", Time.UnixTimestamp())))
                      .With(x => Re.GetSubString(x, @"""sid"":""(.*?)"""));

                if (String.IsNullOrWhiteSpace(channelToken) ||
                    channelToken.Equals("NOTOKEN", StringComparison.InvariantCultureIgnoreCase))
                {
                    channelToken = "NOTOKEN";
                }
                else
                {
                    Chat.Status.IsLoggedIn = true;
                }
            }

            if (String.IsNullOrWhiteSpace(sid) && LeaveCallback != null)
            {
                LeaveCallback(this);
                return;
            }


            webSocket = new WebSocketBase();
            webSocket.PingInterval = 0;

            JoinCallback = callback;

            webSocket.DisconnectHandler = () =>
            {
                if (pingTimer != null)
                {
                    pingTimer.Change(Timeout.Infinite, Timeout.Infinite);
                }

                Leave();
            };

            webSocket.ReceiveMessageHandler = ReadRawMessage;

            webSocket.Path     = String.Format("/socket.io/?EIO=3&transport=websocket&sid={0}", sid);
            webSocket.Port     = "3000";
            webSocket.IsSecure = true;
            webSocket.Origin   = "http://connectcast.tv";
            webSocket.Cookies  = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("io", sid)
            };
            webSocket.Host           = "chat.connectcast.tv";
            webSocket.ConnectHandler = () =>
            {
                if (pingTimer != null)
                {
                    pingTimer.Change(PING_INTERVAL, PING_INTERVAL);
                }

                if (disconnectTimer != null)
                {
                    disconnectTimer.Change(PING_INTERVAL * 2, Timeout.Infinite);
                }

                webSocket.Send("2probe");
            };
            SetupStatsWatcher();
            webSocket.Connect();
        }