瀏覽代碼

not using while(true) equals in much frustration

Yorick Rommers 10 年之前
父節點
當前提交
b8a1261600
共有 2 個文件被更改,包括 8 次插入8 次删除
  1. 4 2
      YJMPD-UWP/Model/ApiHandler.cs
  2. 4 6
      YJMPD-UWP/Model/NetworkHandler.cs

+ 4 - 2
YJMPD-UWP/Model/ApiHandler.cs

@@ -20,6 +20,7 @@ namespace YJMPD_UWP.Model
             Hi,
             Name,
             Picture,
+            Msg,
             GameFound,
             PlayerJoined
         }
@@ -31,6 +32,7 @@ namespace YJMPD_UWP.Model
 
         public void HandleMessage(JObject o)
         {
+            Debug.WriteLine("Reached HandleMessage in API");
             Command c = (Command)Enum.Parse(typeof(Command), o["command"].ToString());
 
             switch(c)
@@ -39,7 +41,8 @@ namespace YJMPD_UWP.Model
                     GameFound();
                     break;
                 case Command.PlayerJoined:
-                    PlayerJoined(o["msg"].ToString());
+                    Debug.WriteLine("Played joined");
+                    PlayerJoined(o[Command.PlayerJoined.ToString()].ToString());
                     break;
                 default:
                     //Do nothing
@@ -50,7 +53,6 @@ namespace YJMPD_UWP.Model
 
         private void PlayerJoined(string username)
         {
-            throw new NotImplementedException();
             //Event will be handled by the game manager
             App.Game.AddPlayer(username);
         }

+ 4 - 6
YJMPD-UWP/Model/NetworkHandler.cs

@@ -83,12 +83,12 @@ namespace YJMPD_UWP.Model
 
             BackgroundReader = Windows.System.Threading.ThreadPool.RunAsync(async (workItem) =>
             {
-                while (workItem.Status == AsyncStatus.Started)
+                while (true)
                 {
                     Debug.WriteLine("Awaiting incoming data...");
                     string data = await App.Network.Read();
                     App.Network.HandleMessage(data);
-                    Task.Delay(TimeSpan.FromMilliseconds(50));
+                    await Task.Delay(TimeSpan.FromMilliseconds(50));
                 }
             });
 
@@ -114,10 +114,8 @@ namespace YJMPD_UWP.Model
         {
             Debug.WriteLine(data);
             JObject o = JObject.Parse(data);
-            if (o["msg"].ToString() == "ok")
-            {
-                App.Api.HandleMessage(o);
-            }
+            App.Api.HandleMessage(o);
+            
         }
     }
 }