From 853f39fb0fe9ebf94f51b266bfff918d254bef08 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 7 Mar 2020 19:34:52 -0600 Subject: [PATCH] polling for messages now uses coroutines --- Assets/LoginInfo.asset | 8 ++-- Assets/Scripts/CameraController.cs | 7 +-- Assets/Scripts/CameraController.cs.meta | 4 +- Assets/Scripts/FallingSpike.cs | 4 +- Assets/Scripts/MainMenu.cs | 6 ++- Assets/Scripts/MainMenu.cs.meta | 7 ++- Assets/Scripts/Networking.cs | 63 ++++++++++++++++++------- Assets/Scripts/Networking.cs.meta | 7 ++- Assets/Scripts/PlayerController.cs | 4 +- Assets/Scripts/PlayerController.cs.meta | 6 ++- 10 files changed, 83 insertions(+), 33 deletions(-) diff --git a/Assets/LoginInfo.asset b/Assets/LoginInfo.asset index e9fa0b9..4e25777 100644 --- a/Assets/LoginInfo.asset +++ b/Assets/LoginInfo.asset @@ -12,9 +12,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: fb331b678c613734fa2ba6013e9cd499, type: 3} m_Name: LoginInfo m_EditorClassIdentifier: - playerName: jordan - password: fartsock + playerName: test + password: startPosition: {x: 0, y: 10, z: 0} - sentLogin: 0 + sentLogin: 1 isLoggedIn: 1 - loginFailed: 0 + loginError: diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs index a8bb676..950f793 100644 --- a/Assets/Scripts/CameraController.cs +++ b/Assets/Scripts/CameraController.cs @@ -8,13 +8,14 @@ public class CameraController : MonoBehaviour { // Start is called before the first frame update void Start() { - networking.Connect(); + if (!networking.isConnected()) { + networking.Connect(); + StartCoroutine(networking.ReadMessages()); + } } // Update is called once per frame void Update() { - networking.CheckForMessages(); - if (player) { transform.position = new Vector3(player.position.x-2, player.position.y+2, player.position.z-10); transform.LookAt(player.transform.position + player.up * 4f); diff --git a/Assets/Scripts/CameraController.cs.meta b/Assets/Scripts/CameraController.cs.meta index dca3a87..f146e16 100644 --- a/Assets/Scripts/CameraController.cs.meta +++ b/Assets/Scripts/CameraController.cs.meta @@ -3,7 +3,9 @@ guid: af7a4f3a5c0c243498985148ee243e45 MonoImporter: externalObjects: {} serializedVersion: 2 - defaultReferences: [] + defaultReferences: + - networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} + - player: {instanceID: 0} executionOrder: 0 icon: {instanceID: 0} userData: diff --git a/Assets/Scripts/FallingSpike.cs b/Assets/Scripts/FallingSpike.cs index d129023..21c1e05 100644 --- a/Assets/Scripts/FallingSpike.cs +++ b/Assets/Scripts/FallingSpike.cs @@ -38,7 +38,7 @@ public class FallingSpike : MonoBehaviour { if (Physics.Raycast(raycastOrigins.left, Vector3.down, out hit, dx, collisionMask) || Physics.Raycast(raycastOrigins.right, Vector3.down, out hit, dx, collisionMask)) { - Debug.LogFormat("the spike hit something: {0}", hit); + // Debug.LogFormat("the spike hit something: {0}", hit); dx = hit.distance; doneFalling = true; isFalling = false; @@ -50,7 +50,7 @@ public class FallingSpike : MonoBehaviour { } void OnTriggerEnter(Collider other) { - Debug.LogFormat("Falling spike collided with other: {0}", other); + // Debug.LogFormat("Falling spike collided with other: {0}", other); PlayerController player = other.GetComponent(); if (player) { StartFalling(); diff --git a/Assets/Scripts/MainMenu.cs b/Assets/Scripts/MainMenu.cs index 2fe960e..98c9c87 100644 --- a/Assets/Scripts/MainMenu.cs +++ b/Assets/Scripts/MainMenu.cs @@ -16,12 +16,14 @@ public class MainMenu : MonoBehaviour void Start() { loginInfo.isLoggedIn = false; loginInfo.sentLogin = false; - networking.Connect(); + if (!networking.isConnected()) { + networking.Connect(); + StartCoroutine(networking.ReadMessages()); + } } // Update is called once per frame void Update() { - networking.CheckForMessages(); usernameField.interactable = !loginInfo.sentLogin; passwordField.interactable = !loginInfo.sentLogin; if (loginInfo.loginError == "") { diff --git a/Assets/Scripts/MainMenu.cs.meta b/Assets/Scripts/MainMenu.cs.meta index 517ea29..eb501f5 100644 --- a/Assets/Scripts/MainMenu.cs.meta +++ b/Assets/Scripts/MainMenu.cs.meta @@ -3,7 +3,12 @@ guid: 2e685bfc1554e0c419dadae2bc4803aa MonoImporter: externalObjects: {} serializedVersion: 2 - defaultReferences: [] + defaultReferences: + - loginInfo: {fileID: 11400000, guid: 3935bc2db09ef5f408342222f9490ded, type: 2} + - networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} + - usernameField: {instanceID: 0} + - passwordField: {instanceID: 0} + - errorText: {instanceID: 0} executionOrder: 0 icon: {instanceID: 0} userData: diff --git a/Assets/Scripts/Networking.cs b/Assets/Scripts/Networking.cs index 380cd00..92a09fb 100644 --- a/Assets/Scripts/Networking.cs +++ b/Assets/Scripts/Networking.cs @@ -46,6 +46,7 @@ public class Networking : ScriptableObject { Debug.LogFormat("Connecting to: {0}", b.Uri); await sock.ConnectAsync(b.Uri, CancellationToken.None); Debug.LogFormat("Finished connection task with status: {0}", sock.State); + return; } public void SendCollectSoul(string playerName, Vector3 position) { @@ -85,23 +86,49 @@ public class Networking : ScriptableObject { sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); } - async public void CheckForMessages() { - if (readTask != null) { - return; - } - if (sock == null) { - return; + public IEnumerator ReadMessages() { + Task readTask = null; // = sock.ReceiveAsync(readBuffer, CancellationToken.None); + + while (true) { + if (!isConnected()) { + yield return null; + continue; + } + + if (readTask == null) { + readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None); + } + + switch (readTask.Status) { + case TaskStatus.Created: + case TaskStatus.WaitingForActivation: + case TaskStatus.WaitingToRun: + case TaskStatus.Running: + case TaskStatus.WaitingForChildrenToComplete: + yield return null; + continue; + + case TaskStatus.RanToCompletion: + parseMessage(readTask.Result); + break; + + case TaskStatus.Canceled: + break; + case TaskStatus.Faulted: + break; + } + readTask = null; + yield return null; } - readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None); - WebSocketReceiveResult result = await readTask; - readTask = null; - string msg = Encoding.UTF8.GetString(readBuffer.Array, 0, result.Count); + } + + private void parseMessage(WebSocketReceiveResult message) { + string msg = Encoding.UTF8.GetString(readBuffer.Array, 0, message.Count); string[] parts = msg.Split(new char[]{' '}, 2); if (parts.Length != 2) { Debug.LogFormat("dunno how to handle this msg: {0}", msg); return; } - switch (parts[0]) { case "spawn-soul": SpawnSoul spawned = JsonUtility.FromJson(parts[1]); @@ -128,15 +155,19 @@ public class Networking : ScriptableObject { } private void onSpawnSoul(SpawnSoul spawn) { - Debug.LogFormat("spawn a soul: {0}", spawn); + Debug.LogFormat("spawn a soul: {0} at {1}", spawn.playerName, spawn.position); GameObject soul = Instantiate(soulPrefab, spawn.position, Quaternion.identity); soul.name = spawn.playerName; GameObject allSouls = GameObject.Find("Souls"); - soul.transform.SetParent(allSouls.transform); + if (allSouls == null) { + Debug.LogError("unable to find souls container!"); + } else { + soul.transform.SetParent(allSouls.transform); - SoulController sc = soul.GetComponent(); - sc.playerName = spawn.playerName; + SoulController sc = soul.GetComponent(); + sc.playerName = spawn.playerName; + } } private void onSoulCollected(CollectSoul collected) { @@ -174,7 +205,7 @@ public class Networking : ScriptableObject { } } - private bool isConnected() { + public bool isConnected() { return sock != null && sock.State == WebSocketState.Open; } diff --git a/Assets/Scripts/Networking.cs.meta b/Assets/Scripts/Networking.cs.meta index 8664dff..f4ecbd2 100644 --- a/Assets/Scripts/Networking.cs.meta +++ b/Assets/Scripts/Networking.cs.meta @@ -3,7 +3,12 @@ guid: 9d1527f3b62a45143867c15cefe61e7b MonoImporter: externalObjects: {} serializedVersion: 2 - defaultReferences: [] + defaultReferences: + - soulPrefab: {fileID: 923676258187016684, guid: 80b838e4e488ed64b9ac358ee000f444, + type: 3} + - playerPrefab: {fileID: 6383496741192187632, guid: cd445451907d7274daea79c67e13a202, + type: 3} + - loginInfo: {instanceID: 0} executionOrder: 0 icon: {instanceID: 0} userData: diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 4bfaec5..af64231 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -54,11 +54,11 @@ public class PlayerController : MonoBehaviour { } void OnCollisionEnter(Collision other) { - Debug.LogFormat("Player collided with {0}", other); + // Debug.LogFormat("Player collided with {0}", other); } void OnTriggerEnter(Collider other) { - Debug.LogFormat("Player triggered other: {0}", other); + // Debug.LogFormat("Player triggered other: {0}", other); if (other.CompareTag("Soul")) { SoulController soul = other.GetComponent(); networking.SendCollectSoul(soul.playerName, other.transform.position); diff --git a/Assets/Scripts/PlayerController.cs.meta b/Assets/Scripts/PlayerController.cs.meta index 9808fa1..db76409 100644 --- a/Assets/Scripts/PlayerController.cs.meta +++ b/Assets/Scripts/PlayerController.cs.meta @@ -3,7 +3,11 @@ guid: 7e392b35ca7959444b61a3e29a242f2b MonoImporter: externalObjects: {} serializedVersion: 2 - defaultReferences: [] + defaultReferences: + - hud: {instanceID: 0} + - soulPrefab: {fileID: 923676258187016684, guid: 80b838e4e488ed64b9ac358ee000f444, + type: 3} + - networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} executionOrder: 0 icon: {instanceID: 0} userData: