public MainViewModel(DeviceSocketConnection deviceSocketConnection)
 {
     m_deviceSocketConnection = deviceSocketConnection;
     deviceSocketConnection.OnMessageReceived += DeviceSocketConnection_OnMessageReceived;
     deviceSocketConnection.OnSocketClosed    += DeviceSocketConnection_OnSocketClosed;
     deviceSocketConnection.RunReceiveLoop();
 }
        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private async void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            await DeviceSocketConnection.CloseAnySocketAsync();

            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
        public async Task ConnectAsync(string mothershipName, Action onNameAssignedAction)
        {
            m_onNameAssignedAction   = onNameAssignedAction;
            m_deviceSocketConnection = await DeviceSocketConnection.CreateAsync(WebUrls.ClientSocketUrl(mothershipName));

            m_deviceSocketConnection.OnMessageReceived += M_deviceSocketConnection_OnMessageReceived;
            m_deviceSocketConnection.OnSocketClosed    += M_deviceSocketConnection_OnSocketClosed;
            m_deviceSocketConnection.RunReceiveLoop();
        }
        private void M_deviceSocketConnection_OnSocketClosed(object sender, EventArgs e)
        {
            m_deviceSocketConnection.OnSocketClosed    -= M_deviceSocketConnection_OnSocketClosed;
            m_deviceSocketConnection.OnMessageReceived -= M_deviceSocketConnection_OnMessageReceived;
            m_deviceSocketConnection = null;

            var dontWait = SimpleDispatcher.RunAsync(delegate
            {
                try
                {
                    OnConnectionClosed?.Invoke("Socket closed");
                }
                catch { }
            });
        }
        public static async Task <MainViewModel> CreateAsync(ConnectingToWebAppPage connectingPage)
        {
            connectingPage.WriteLog("Loading host config...");
            CardViewModel.HOST_CONFIG_JSON = await FileIO.ReadTextAsync(await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///HostConfigs/HostConfig.json")));

            DeviceSocketConnection deviceSocketConnection;

            try
            {
                connectingPage.WriteLog("Connecting to socket...");
                deviceSocketConnection = await DeviceSocketConnection.CreateAsync(WebUrls.MOTHERSHIP_SOCKET_URL);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to connect to the web server: " + WebUrls.MOTHERSHIP_SOCKET_URL + "\n\n" + ex.ToString());
            }

            connectingPage.WriteLog("Socket connected!");

            var mainViewModel = new MainViewModel(deviceSocketConnection);

            connectingPage.WriteLog("Getting cards folder...");
            var cardsFolder = await Package.Current.InstalledLocation.GetFolderAsync("Cards");

            connectingPage.WriteLog("Enumerating cards...");
            foreach (var file in await cardsFolder.GetFilesAsync())
            {
                if (file.FileType.ToLower().Equals(".json"))
                {
                    connectingPage.WriteLog("Loading card payload...");
                    mainViewModel.GalleryCards.Add(new CardViewModel()
                    {
                        Name     = file.DisplayName,
                        CardJson = await FileIO.ReadTextAsync(file)
                    });
                }
            }

            mainViewModel.QueuedAndCurrentCards.Add(mainViewModel.FindNewCardFromGallery());
            mainViewModel.QueuedAndCurrentCards.Add(mainViewModel.FindNewCardFromGallery());
            mainViewModel.CurrentCard = mainViewModel.QueuedAndCurrentCards.Last();

            // Start the loop
            connectingPage.WriteLog("Starting the card loop...");
            mainViewModel.CycleLoop();

            return(mainViewModel);
        }