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(); }
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 }