|
|
@ -46,6 +46,7 @@ public class Networking : ScriptableObject {
|
|
|
|
Debug.LogFormat("Connecting to: {0}", b.Uri);
|
|
|
|
Debug.LogFormat("Connecting to: {0}", b.Uri);
|
|
|
|
await sock.ConnectAsync(b.Uri, CancellationToken.None);
|
|
|
|
await sock.ConnectAsync(b.Uri, CancellationToken.None);
|
|
|
|
Debug.LogFormat("Finished connection task with status: {0}", sock.State);
|
|
|
|
Debug.LogFormat("Finished connection task with status: {0}", sock.State);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SendCollectSoul(string playerName, Vector3 position) {
|
|
|
|
public void SendCollectSoul(string playerName, Vector3 position) {
|
|
|
@ -85,23 +86,49 @@ public class Networking : ScriptableObject {
|
|
|
|
sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
|
sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async public void CheckForMessages() {
|
|
|
|
public IEnumerator ReadMessages() {
|
|
|
|
if (readTask != null) {
|
|
|
|
Task<WebSocketReceiveResult> readTask = null; // = sock.ReceiveAsync(readBuffer, CancellationToken.None);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
if (sock == null) {
|
|
|
|
if (!isConnected()) {
|
|
|
|
return;
|
|
|
|
yield return null;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (readTask == null) {
|
|
|
|
readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None);
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
string[] parts = msg.Split(new char[]{' '}, 2);
|
|
|
|
if (parts.Length != 2) {
|
|
|
|
if (parts.Length != 2) {
|
|
|
|
Debug.LogFormat("dunno how to handle this msg: {0}", msg);
|
|
|
|
Debug.LogFormat("dunno how to handle this msg: {0}", msg);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (parts[0]) {
|
|
|
|
switch (parts[0]) {
|
|
|
|
case "spawn-soul":
|
|
|
|
case "spawn-soul":
|
|
|
|
SpawnSoul spawned = JsonUtility.FromJson<SpawnSoul>(parts[1]);
|
|
|
|
SpawnSoul spawned = JsonUtility.FromJson<SpawnSoul>(parts[1]);
|
|
|
@ -128,16 +155,20 @@ public class Networking : ScriptableObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void onSpawnSoul(SpawnSoul spawn) {
|
|
|
|
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);
|
|
|
|
GameObject soul = Instantiate(soulPrefab, spawn.position, Quaternion.identity);
|
|
|
|
soul.name = spawn.playerName;
|
|
|
|
soul.name = spawn.playerName;
|
|
|
|
|
|
|
|
|
|
|
|
GameObject allSouls = GameObject.Find("Souls");
|
|
|
|
GameObject allSouls = GameObject.Find("Souls");
|
|
|
|
|
|
|
|
if (allSouls == null) {
|
|
|
|
|
|
|
|
Debug.LogError("unable to find souls container!");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
soul.transform.SetParent(allSouls.transform);
|
|
|
|
soul.transform.SetParent(allSouls.transform);
|
|
|
|
|
|
|
|
|
|
|
|
SoulController sc = soul.GetComponent<SoulController>();
|
|
|
|
SoulController sc = soul.GetComponent<SoulController>();
|
|
|
|
sc.playerName = spawn.playerName;
|
|
|
|
sc.playerName = spawn.playerName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void onSoulCollected(CollectSoul collected) {
|
|
|
|
private void onSoulCollected(CollectSoul collected) {
|
|
|
|
Debug.LogFormat("a soul was collected: {0}", 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;
|
|
|
|
return sock != null && sock.State == WebSocketState.Open;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|