polling for messages now uses coroutines

master
Jordan Orelli 5 years ago
parent fcaf7122ea
commit 853f39fb0f

@ -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:

@ -8,13 +8,14 @@ public class CameraController : MonoBehaviour {
// Start is called before the first frame update
void Start() {
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);

@ -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:

@ -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<PlayerController>();
if (player) {
StartFalling();

@ -16,12 +16,14 @@ public class MainMenu : MonoBehaviour
void Start() {
loginInfo.isLoggedIn = false;
loginInfo.sentLogin = false;
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 == "") {

@ -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:

@ -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<WebSocketReceiveResult> readTask = null; // = sock.ReceiveAsync(readBuffer, CancellationToken.None);
while (true) {
if (!isConnected()) {
yield return null;
continue;
}
if (readTask == null) {
readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None);
WebSocketReceiveResult result = await readTask;
}
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;
string msg = Encoding.UTF8.GetString(readBuffer.Array, 0, result.Count);
yield return null;
}
}
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<SpawnSoul>(parts[1]);
@ -128,16 +155,20 @@ 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");
if (allSouls == null) {
Debug.LogError("unable to find souls container!");
} else {
soul.transform.SetParent(allSouls.transform);
SoulController sc = soul.GetComponent<SoulController>();
sc.playerName = spawn.playerName;
}
}
private void onSoulCollected(CollectSoul collected) {
Debug.LogFormat("a soul was collected: {0}", collected);
@ -174,7 +205,7 @@ public class Networking : ScriptableObject {
}
}
private bool isConnected() {
public bool isConnected() {
return sock != null && sock.State == WebSocketState.Open;
}

@ -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:

@ -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<SoulController>();
networking.SendCollectSoul(soul.playerName, other.transform.position);

@ -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:

Loading…
Cancel
Save