コード例 #1
0
        public override async Task Connect()
        {
            OnConnecting(EventArgs.Empty);

            if (string.IsNullOrEmpty(Address))
            {
                OnConnectError(new ConnectionErrorEventArgs(this, new Exception("Address is empty")));
                return;
            }
            if (UseSocialLogin)
            {
                if (string.IsNullOrEmpty(UserId))
                {
                    OnConnectError(new ConnectionErrorEventArgs(this, new NotAuthenticatedException("Not authenticated to this server")));
                    return;
                }
            }
            else if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            {
                OnConnectError(new ConnectionErrorEventArgs(this, new NotAuthenticatedException("Username or password are not specified")));
                return;
            }

            //ServicePointManager.FindServicePoint (new Uri (Address)).ConnectionLimit = 100;

            // force long polling on mono, until SSE works reliably
            Func <IClientTransport> transport;

            /*if (EtoEnvironment.Platform.IsMono)
             *  transport = () => new LongPollingTransport();
             * else*/
            transport = () => new AutoTransport(new DefaultHttpClient());

            Client = new jab.JabbRClient(Address, transport);
#if DEBUG
            var settings = Path.Combine(EtoEnvironment.GetFolderPath(EtoSpecialFolder.ApplicationSettings), "jabbr.log");
            Client.TraceWriter = new TextWriterTraceListener(settings).Writer;
            Client.TraceLevel  = TraceLevels.All;
#endif

            if (UseSocialLogin)
            {
                throw new NotSupportedException();
            }

            bool connected = false;
            try
            {
                var logOnInfo = await Client.Connect(UserName, Password);

                highlighRegex = null;
                connected     = true;
                HookupEvents();

                OnGlobalMessageReceived(new NotificationEventArgs(new NotificationMessage(string.Format("Using {0} transport", Client.Connection.Transport.Name))));
                var userInfo = await Client.GetUserInfo();

                CurrentUser  = new JabbRUser(this, userInfo);
                loadingRooms = logOnInfo.Rooms.Select(r => new JabbRRoom(this, r)).ToList();
                InitializeChannels(loadingRooms);

                if (EtoEnvironment.Platform.IsMono)
                {
                    var keepAliveTime = TimeSpan.FromMinutes(5);
                    if (timer != null)
                    {
                        timer.Dispose();
                    }
                    timer = new Timer(state => Client.Send(new jm.ClientMessage
                    {
                        Id      = Guid.NewGuid().ToString(),
                        Content = string.Format("/where {0}", CurrentUser.Name)
                    }), null, keepAliveTime, keepAliveTime);
                }

                OnConnected(EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Debug.Print(string.Format("Error: {0}", ex.GetBaseException().Message));
                OnConnectError(new ConnectionErrorEventArgs(this, ex));
                if (connected)
                {
                    Client.Disconnect();
                }
            }

            // load all room initial channel info/history
            while (true)
            {
                JabbRRoom room;
                lock (loadingRooms)
                {
                    if (loadingRooms.Count > 0)
                    {
                        room = loadingRooms[0];
                        loadingRooms.Remove(room);
                    }
                    else
                    {
                        break;
                    }
                }
                //Debug.WriteLine(string.Format("Loading messages for room {0}", room.Name));
                await room.LoadRoomInfo();
            }
        }
コード例 #2
0
        public override async Task Connect()
        {
            OnConnecting(EventArgs.Empty);

            if (string.IsNullOrEmpty(Address))
            {
                OnConnectError(new ConnectionErrorEventArgs(this, new Exception("Address is empty")));
                return;
            }
            if (UseSocialLogin)
            {
                if (string.IsNullOrEmpty(UserId))
                {
                    OnConnectError(new ConnectionErrorEventArgs(this, new NotAuthenticatedException("Not authenticated to this server")));
                    return;
                }
            }
            else if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            {
                OnConnectError(new ConnectionErrorEventArgs(this, new NotAuthenticatedException("Username or password are not specified")));
                return;
            }

            //ServicePointManager.FindServicePoint (new Uri (Address)).ConnectionLimit = 100;

            // force long polling on mono, until SSE works reliably
            Func<IClientTransport> transport;
            /*if (EtoEnvironment.Platform.IsMono)
                transport = () => new LongPollingTransport();
            else*/
            transport = () => new AutoTransport(new DefaultHttpClient());

            Client = new jab.JabbRClient(Address, transport);
#if DEBUG
            var settings = Path.Combine (EtoEnvironment.GetFolderPath (EtoSpecialFolder.ApplicationSettings), "jabbr.log");
            Client.TraceWriter = new TextWriterTraceListener (settings).Writer;
            Client.TraceLevel = TraceLevels.All;
#endif

            if (UseSocialLogin)
            {
                throw new NotSupportedException();
            }

            bool connected = false;
            try
            {
                var logOnInfo = await Client.Connect(UserName, Password);
                highlighRegex = null;
                connected = true;
                HookupEvents();

                OnGlobalMessageReceived(new NotificationEventArgs(new NotificationMessage(string.Format("Using {0} transport", Client.Connection.Transport.Name))));
                var userInfo = await Client.GetUserInfo();
                CurrentUser = new JabbRUser(this, userInfo);
                loadingRooms = logOnInfo.Rooms.Select(r => new JabbRRoom(this, r)).ToList();
                InitializeChannels(loadingRooms);

                if (EtoEnvironment.Platform.IsMono)
                {
                    var keepAliveTime = TimeSpan.FromMinutes(5);
                    if (timer != null)
                        timer.Dispose();
                    timer = new Timer(state => Client.Send(new jm.ClientMessage
                    {
                        Id = Guid.NewGuid().ToString(),
                        Content = string.Format("/where {0}", CurrentUser.Name)
                    }), null, keepAliveTime, keepAliveTime);
                }

                OnConnected(EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Debug.Print(string.Format("Error: {0}", ex.GetBaseException().Message));
                OnConnectError(new ConnectionErrorEventArgs(this, ex));
                if (connected)
                    Client.Disconnect();
            }

            // load all room initial channel info/history
            while (true)
            {
                JabbRRoom room;
                lock (loadingRooms)
                {
                    if (loadingRooms.Count > 0)
                    {
                        room = loadingRooms[0];
                        loadingRooms.Remove(room);
                    }
                    else
                        break;
                }
                //Debug.WriteLine(string.Format("Loading messages for room {0}", room.Name));
                await room.LoadRoomInfo();
            }
        }