예제 #1
0
        public static void GetBarrageServerInfo(int roomId, out IPEndPoint[] barrageServers, out int groupId)
        {
            barrageServers = null;
            groupId        = 0;

            RoomId = roomId;

            // 如果计时器已经启动, 先停掉它, 确保其不能发送数据
            if (_keepliveTimer != null)
            {
                _keepliveTimer.Stop();
            }

            // 获取斗鱼服务器
            LogService.Info("获取斗鱼服务器");
            var servers = GetServers(roomId);

            // 连接斗鱼服务器
            Exception exception = null;

            foreach (var server in servers)
            {
                try {
                    LogService.InfoFormat("连接到斗鱼服务器: {0}", server.ToString());
                    exception    = null;
                    _douyuSocket = new DouyuSocket();
                    var host = server.Split(':')[0];
                    var port = int.Parse(server.Split(':')[1]);
                    _douyuSocket.Connect(host, port);
                } catch (Exception ex) {
                    exception = ex;
                }
                if (exception == null)
                {
                    break;
                }
            }
            if (exception != null)
            {
                throw new DouyuException("连接斗鱼服务器失败!", exception);
            }

            LogService.Info("发送登录消息");
            _douyuSocket.SendMessage(new LoginreqMessage(roomId));

            LogService.Info("获取弹幕服务器");
            barrageServers = GetBarrageServers();

            groupId = -9999;    // 海量弹幕
            //LogService.Info("获取弹幕分组");
            //groupId = GetMessageGroup();

            if (_keepliveTimer == null)
            {
                _keepliveTimer          = new System.Timers.Timer(45 * 1000);
                _keepliveTimer.Elapsed += KeepliveTimer_Elapsed;
                _keepliveTimer.Start();
            }
        }
        void ConnectBarrageServer()
        {
            // 获取弹幕服务器消息
            IPEndPoint[] barrageServers;
            int          messageGroup;

            DouyuService.GetBarrageServerInfo(RoomId, out barrageServers, out messageGroup);

            // 连接弹幕服务器
            Exception exception = null;

            _douyuSocket = new DouyuSocket();
            _douyuSocket.ClientMessageSent += OnClientMessageSent;
            foreach (var server in barrageServers)
            {
                try {
                    exception = null;
                    LogService.InfoFormat("连接到弹幕服务器: {0}", server.ToString());
                    _douyuSocket.Connect(server);
                } catch (Exception ex) {
                    exception = ex;
                }
                if (exception == null)
                {
                    break;
                }
            }
            if (exception != null)
            {
                throw new DouyuException("连接弹幕服务器失败!", exception);
            }

            // 登录房间&入组
            LoginRoom();
            JoinGroup(messageGroup);
        }