public UserBL() { server = ServerBL.Instance; server.userHubProxy.On("LogInNotificated", (string userName) => OnLoggedIn?.Invoke(userName)); server.userHubProxy.On("LogOutNotificated", (string userName) => OnLoggedOut?.Invoke(userName)); server.chatHubProxy.On("addToRoom", (string room) => OnRoomOpen?.Invoke(room)); }
public void LogOut() { var acct = SecureStorageStore.FindAccountsForServiceAsync(Configuration.GoogleServiceName)?.Result?.FirstOrDefault(); SecureStorageStore.SaveAsync(null, Configuration.GoogleServiceName).Wait(); _loggedIn = null; OnLoggedOut?.Invoke(this, new EventArgs()); }
public void Logout() { if (LoggedIn) { LoggedIn = false; Socket.Close(); OnLoggedOut?.Invoke(this, new UserEventArgs(User)); } }
public void Login(string token) { if (Token != token) { Token = token; } Socket = new WebSocket("wss://gateway.discord.gg/?v=6&encoding=json"); if (Config.Proxy != null) { if (Config.Proxy.Type == ProxyType.HTTP) //WebSocketSharp only supports HTTP proxies :( { Socket.SetProxy("http://" + Config.Proxy.Host + Config.Proxy.Port, Config.Proxy.Username, Config.Proxy.Password); } } Socket.OnMessage += SocketDataReceived; Socket.OnClose += (sender, e) => { Reset(); if (e.Code >= (ushort)GatewayCloseError.UnknownError) { GatewayCloseError err = (GatewayCloseError)e.Code; if (err != GatewayCloseError.RateLimited && err != GatewayCloseError.SessionTimedOut && err != GatewayCloseError.UnknownError) { if (LoggedIn) { LoggedIn = false; OnLoggedOut?.Invoke(this, new LogoutEventArgs(err)); } } } if (LoggedIn) { while (true) { try { Login(Token); return; } catch { Thread.Sleep(100); } } } }; Socket.Connect(); }
private async Task LogoutHandle(MouseEventArgs args) { var logoutCancelEventArgs = new LoginCancelEventArgs(); await OnLoggingOut.InvokeAsync(logoutCancelEventArgs); if (!logoutCancelEventArgs.Cancel) { await OnLoggedOut.InvokeAsync(logoutCancelEventArgs); if (LogoutAction == Redirect) { NavigationManager.NavigateTo(LogoutPageUrl); } } }
public static void RegisterEvent() { _loggedIn = UnityConnect.instance.loggedIn; UnityConnect.instance.StateChanged += state => { if (_loggedIn != state.loggedIn) { if (state.loggedIn) { OnLoggedIn?.Invoke(); } else { OnLoggedOut?.Invoke(); } _loggedIn = state.loggedIn; } }; if (_loggedIn) { OnLoggedIn?.Invoke(); } }
void Handle_OnSuccessfulLogin(object sender, bool e) { LoggedIn = true; void action() { MasterDetailNav = new FreshMasterDetailNavigationContainer(); MasterDetailNav.Init(""); var page = FreshPageModelResolver.ResolvePageModel <MasterMenuPageModel>(); ((MasterMenuPageModel)page.BindingContext).OnLogout += (_sender, _e) => { var __page = FreshPageModelResolver.ResolvePageModel <LoginPageModel>(true); MainPage = __page; MasterDetailNav = null; MasterDetailContainer = null; UserName = null; ((LoginPageModel)__page.BindingContext).OnSuccessfulLogin += Handle_OnSuccessfulLogin; try { OnLoggedOut?.Invoke(this, true); } catch (Exception ex) { Crashes.TrackError(ex); } }; page.Title = "Menu"; MasterDetailNav.Master = page; MasterDetailNav.MasterBehavior = MasterBehavior.Popover; MasterDetailNav.AddPage <WelcomePageModel>("Home", null); CurrentPageModel = MasterDetailNav.Pages.Values.First().BindingContext as BaseDetailPage; MasterDetailNav.AddPage <MaintenanceTicketsPageModel>("Maintenance Tickets", null); this.MainPage = MasterDetailNav; MasterDetailContainer = MasterDetailNav; MasterDetailNav.IsPresentedChanged += (_sender, _e) => { if (_sender is FreshMasterDetailNavigationContainer) { foreach (var _page in ((FreshMasterDetailNavigationContainer)_sender).Pages.Values) { var nav = _page as NavigationPage; if (nav.CurrentPage is null || nav.CurrentPage.BindingContext is null) { return; } if (nav.CurrentPage.BindingContext is BaseDetailPage currentPageModel) { if (!currentPageModel.IsModal) { currentPageModel.HamburgerIsVisible = !MasterDetailNav.IsPresented; } else { currentPageModel.HamburgerIsVisible = false; } CurrentPageModel = currentPageModel; } } } }; } Xamarin.Forms.Device.BeginInvokeOnMainThread(action); }
public DiscordSocketClient(DiscordSocketConfig config = null) : base() { RequestLock = new object(); if (config == null) { config = new DiscordSocketConfig(); } Config = new LockedSocketConfig(config); base.Config = Config; FinishConfig(); if (Config.Cache) { GuildCache = new ConcurrentDictionary <ulong, SocketGuild>(); PrivateChannels = new ConcurrentList <PrivateChannel>(); Presences = new ConcurrentDictionary <ulong, DiscordPresence>(); VoiceStates = new AutoConcurrentDictionary <ulong, DiscordVoiceStateContainer>((userId) => new DiscordVoiceStateContainer(userId)); GuildSettings = new ConcurrentDictionary <ulong, ClientGuildSettings>(); PrivateChannelSettings = new List <DiscordChannelSettings>(); ClientMembers = new ConcurrentDictionary <ulong, GuildMember>(); } WebSocket = new DiscordWebSocket <GatewayOpcode>($"wss://gateway.discord.gg/?v={Config.ApiVersion}&encoding=json"); WebSocket.OnClosed += (s, args) => { State = GatewayConnectionState.NotConnected; Reset(); bool lostConnection = args.Code == 1006 || args.Code == 1001; if (lostConnection) { Thread.Sleep(200); } GatewayCloseCode err = (GatewayCloseCode)args.Code; if (LoggedIn && (lostConnection || err == GatewayCloseCode.RateLimited || err == GatewayCloseCode.SessionTimedOut || err == GatewayCloseCode.UnknownError)) { Login(Token); } else { OnLoggedOut?.Invoke(this, new LogoutEventArgs(err, args.Reason)); } }; WebSocket.OnMessageReceived += WebSocket_OnMessageReceived; VoiceClients = new VoiceClientDictionary(this); OnMediaServer += (s, e) => { if (e.StreamKey == null) { if (e.Guild == null) { VoiceClients.Private.SetServer(e); } else { VoiceClients[e.Guild.Id].SetServer(e); } } else { var key = new StreamKey(e.StreamKey); VoiceClients[key.GuildId].Livestream.SetSessionServer(key.UserId, e); } }; }
public DiscordSocketClient(DiscordSocketConfig config = null) : base() { RequestLock = new object(); if (config == null) { config = new DiscordSocketConfig(); } Config = new LockedSocketConfig(config); base.Config = Config; FinishConfig(); if (Config.Cache) { GuildCache = new ConcurrentDictionary <ulong, SocketGuild>(); PrivateChannels = new ConcurrentList <PrivateChannel>(); VoiceStates = new AutoConcurrentDictionary <ulong, DiscordVoiceStateContainer>((userId) => new DiscordVoiceStateContainer(userId)); GuildSettings = new ConcurrentDictionary <ulong, ClientGuildSettings>(); PrivateChannelSettings = new List <DiscordChannelSettings>(); } VoiceSessions = new ConcurrentDictionary <ulong, DiscordVoiceSession>(); Livestreams = new ConcurrentDictionary <string, DiscordGoLiveSession>(); WebSocket = new DiscordWebSocket <GatewayOpcode>($"wss://gateway.discord.gg/?v={Config.ApiVersion}&encoding=json"); WebSocket.OnClosed += (s, args) => { State = GatewayConnectionState.NotConnected; Reset(); bool lostConnection = args.Code == 1006 || args.Code == 1001; if (lostConnection) { Thread.Sleep(200); } GatewayCloseCode err = (GatewayCloseCode)args.Code; if (LoggedIn && (lostConnection || err == GatewayCloseCode.RateLimited || err == GatewayCloseCode.SessionTimedOut || err == GatewayCloseCode.UnknownError)) { Login(Token); } else { OnLoggedOut?.Invoke(this, new LogoutEventArgs(err, args.Reason)); } }; WebSocket.OnMessageReceived += WebSocket_OnMessageReceived; #region media event handlers OnSessionVoiceState += (c, state) => { lock (VoiceSessions.Lock) { foreach (var session in VoiceSessions.Values) { if (session.GuildId == (state.Guild == null ? null : (ulong?)state.Guild.Id)) { if (state.Channel == null || session.SessionId != state.SessionId) { session.Kill(); } else if (state.SessionId == session.SessionId) { session.ChannelId = state.Channel.Id; } break; } } } }; OnMediaServer += (c, args) => { if (args.StreamKey == null) { lock (VoiceSessions.Lock) { foreach (var session in VoiceSessions.Values) { if (args.GuildId == session.GuildId) { session.UpdateServer(args); break; } } } } else if (Livestreams.TryGetValue(args.StreamKey, out DiscordGoLiveSession session)) { args.GuildId = session.Guild.Id; session.UpdateServer(args); if (StreamKey.Deserialize(args.StreamKey).UserId == User.Id) { session.ParentSession.Livestream = session; } else { session.ParentSession.WatchingDictionary[args.StreamKey] = session; } } }; OnStreamUpdated += (c, update) => { if (Livestreams.TryGetValue(update.StreamKey, out DiscordGoLiveSession session)) { session.Update(update); } }; OnStreamDeleted += (c, delete) => { if (Livestreams.TryGetValue(delete.StreamKey, out DiscordGoLiveSession session)) { Livestreams.Remove(delete.StreamKey); if (delete.StreamKey.Split(':').Last() == User.Id.ToString()) { session.ParentSession.Livestream = null; } else { session.ParentSession.WatchingDictionary.Remove(delete.StreamKey); } session.Disconnect(delete); } }; #endregion }
static void SetClientEvents() { client.Log += Client_Log; client.Ready += Client_Ready; client.MessageReceived += MessageReceived; client.ReactionAdded += Client_ReactionAdded; client.ReactionRemoved += Client_ReactionRemoved; client.JoinedGuild += Client_JoinedGuild; client.ChannelCreated += (SocketChannel arg) => { OnChannelCreated?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.ChannelDestroyed += (SocketChannel arg) => { OnChannelDestroyed?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.ChannelUpdated += (SocketChannel arg1, SocketChannel arg2) => { OnChannelUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.CurrentUserUpdated += (SocketSelfUser arg1, SocketSelfUser arg2) => { OnCurrentUserUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.GuildAvailable += (SocketGuild arg) => { OnGuildAvailable?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.GuildMembersDownloaded += (SocketGuild arg) => { OnGuildMembersDownloaded?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.GuildMemberUpdated += (Cacheable <SocketGuildUser, ulong> arg1, SocketGuildUser arg2) => { OnGuildMemberUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.GuildUnavailable += (SocketGuild arg) => { OnGuildUnavailable?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.GuildUpdated += (SocketGuild arg1, SocketGuild arg2) => { OnGuildUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.LatencyUpdated += (int arg1, int arg2) => { OnLatencyUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.LeftGuild += (SocketGuild arg) => { OnLeftGuild?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.LoggedIn += () => { OnLoggedIn?.InvokeParallel(); return(Task.FromResult(default(object))); }; client.LoggedOut += () => { OnLoggedOut?.InvokeParallel(); return(Task.FromResult(default(object))); }; client.MessageDeleted += (Cacheable <IMessage, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnMessageDeleted?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.MessagesBulkDeleted += (IReadOnlyCollection <Cacheable <IMessage, ulong> > arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnMessagesBulkDeleted?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.MessageUpdated += (Cacheable <IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3) => { OnMessageUpdated?.InvokeParallel(arg1, arg2, arg3); return(Task.FromResult(default(object))); }; client.ReactionsCleared += (Cacheable <IUserMessage, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnReactionsCleared?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.RecipientAdded += (SocketGroupUser arg) => { OnRecipientAdded?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RecipientRemoved += (SocketGroupUser arg) => { OnRecipientRemoved?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RoleCreated += (SocketRole arg) => { OnRoleCreated?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RoleDeleted += (SocketRole arg) => { OnRoleDeleted?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.RoleUpdated += (SocketRole arg1, SocketRole arg2) => { OnRoleUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserBanned += (SocketUser arg1, SocketGuild arg2) => { OnUserBanned?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserIsTyping += (Cacheable <IUser, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2) => { OnUserIsTyping?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserJoined += (SocketGuildUser arg) => { OnUserJoined?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; client.UserLeft += (SocketGuild arg1, SocketUser arg2) => { OnUserLeft?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserUnbanned += (SocketUser arg1, SocketGuild arg2) => { OnUserUnbanned?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserUpdated += (SocketUser arg1, SocketUser arg2) => { OnUserUpdated?.InvokeParallel(arg1, arg2); return(Task.FromResult(default(object))); }; client.UserVoiceStateUpdated += (SocketUser arg1, SocketVoiceState arg2, SocketVoiceState arg3) => { OnUserVoiceStateUpdated?.InvokeParallel(arg1, arg2, arg3); return(Task.FromResult(default(object))); }; client.VoiceServerUpdated += (SocketVoiceServer arg) => { OnVoiceServerUpdated?.InvokeParallel(arg); return(Task.FromResult(default(object))); }; }