public void StartCollect()
        {
            if (IsCollecting)
            {
                var passwordBox = new PasswordBox();
                if (passwordBox.Show("房间{0}正在收集中!\n如果要取消收集, 请输入密码!", RoomId) == DialogResult.Cancel)
                {
                    Environment.Exit(0);
                }

                if (passwordBox.Password != "123456")
                {
                    MessageBox.Show("密码错误", "开始收集");
                    Environment.Exit(0);
                }
            }

            ConnectBarrageServer();

            IsCollecting = true;
            _stopCollect = false;
            var messageText = "";

            while (!_stopCollect)
            {
                try {
                    TryKeepLive();

                    if (_douyuSocket.TryGetMessage(out messageText))
                    {
                        ProcessMessage(messageText);
                    }
                    else
                    {
                        MyApplication.Delay(100);
                    }
                    //throw new SocketException();
                } catch (Exception ex) {
                    LogService.Error("获取&处理消息异常!", ex);
                    if (ex is SocketException || ex is ObjectDisposedException)
                    {
                        try {
                            LogService.Error("开始重新连接!");
                            if (_douyuSocket != null)
                            {
                                _douyuSocket.Disconnect();
                            }
                            ConnectBarrageServer();
                        } catch (Exception reconnectEx) {
                            LogService.Error("断线重连失败!", reconnectEx);
                            LogService.Error("等待3秒");
                            MyApplication.Delay(3000);
                        }
                    }
                }
            }
            IsCollecting = false;
        }
Ejemplo n.º 2
0
        static void KeepliveTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try {
                _douyuSocket.SendMessage(new KeepliveMessage());
                // 服务器会发送数据过来, 收了吧
                var messageText = "";
                _douyuSocket.TryGetMessage(out messageText);
            } catch (Exception ex) {
                LogService.Error("发送心跳信息失败!", ex);

                _keepliveTimer.Stop();
                _keepliveTimer.Elapsed -= KeepliveTimer_Elapsed;
                _keepliveTimer          = null;

                IPEndPoint[] barrageServers;
                int          groupId;
                GetBarrageServerInfo(RoomId, out barrageServers, out groupId);
            }
        }