receiving messages

master
Jordan Orelli 5 years ago
parent e1caedcccb
commit 30495c8302

File diff suppressed because it is too large Load Diff

@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI; using UnityEngine.UI;
public class MainMenu : MonoBehaviour public class MainMenu : MonoBehaviour
@ -28,5 +29,6 @@ public class MainMenu : MonoBehaviour
usernameField.interactable = false; usernameField.interactable = false;
passwordField.interactable = false; passwordField.interactable = false;
networking.SendLogin(username, password); networking.SendLogin(username, password);
SceneManager.LoadScene("MainLevel");
} }
} }

@ -17,15 +17,22 @@ public class Networking : ScriptableObject {
public int port; public int port;
private ClientWebSocket sock; private ClientWebSocket sock;
private Task connectTask;
private Task writeTask; private Task writeTask;
private Task<WebSocketReceiveResult> readTask;
private ArraySegment<byte> readBuffer;
private int seq; private int seq;
public void Connect() { async public void Connect() {
if (isConnected()) { if (sock != null) {
return; if (sock.State == WebSocketState.Open) {
return;
}
sock = null;
} }
readBuffer = new ArraySegment<byte>(new byte[32000]);
Application.quitting += autoDisconnect; Application.quitting += autoDisconnect;
sock = new ClientWebSocket(); sock = new ClientWebSocket();
UriBuilder b = new UriBuilder(); UriBuilder b = new UriBuilder();
b.Scheme = "ws"; b.Scheme = "ws";
@ -33,10 +40,8 @@ public class Networking : ScriptableObject {
b.Port = port; b.Port = port;
b.Path = path; b.Path = path;
Debug.LogFormat("Connecting to: {0}", b.Uri); Debug.LogFormat("Connecting to: {0}", b.Uri);
connectTask = sock.ConnectAsync(b.Uri, CancellationToken.None); await sock.ConnectAsync(b.Uri, CancellationToken.None);
connectTask.ContinueWith((x) => { Debug.LogFormat("Finished connection task with status: {0}", sock.State);
Debug.LogFormat("Finished connection task with status: {0}", sock.State);
});
} }
public void SendCollectSoul(string playerName, Vector3 position) { public void SendCollectSoul(string playerName, Vector3 position) {
@ -74,8 +79,24 @@ public class Networking : ScriptableObject {
sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None);
} }
async public void CheckForMessages() {
if (readTask != null) {
return;
}
Debug.Log("checking for messages");
readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None);
WebSocketReceiveResult result = await readTask;
readTask = null;
Debug.LogFormat("received message: {0}", result);
Debug.LogFormat("Readbuffer count: {0} offset: {1}", readBuffer.Count, readBuffer.Offset);
string msg = Encoding.UTF8.GetString(readBuffer.Array, 0, result.Count);
string[] parts = msg.Split(new char[]{' '}, 2);
Debug.LogFormat("first: {0} second: {1}", parts[0], parts[1]);
}
private void autoDisconnect() { private void autoDisconnect() {
if (isConnected()) { if (isConnected()) {
Debug.Log("disconnecting websocket via autoDisconnect");
Task task = sock.CloseAsync(WebSocketCloseStatus.NormalClosure, "closed", CancellationToken.None); Task task = sock.CloseAsync(WebSocketCloseStatus.NormalClosure, "closed", CancellationToken.None);
task.Wait(); task.Wait();
} }

@ -30,6 +30,8 @@ public class PlayerController : MonoBehaviour {
} }
void Update() { void Update() {
networking.CheckForMessages();
if (moveController.collisions.above || moveController.collisions.below) { if (moveController.collisions.above || moveController.collisions.below) {
velocity.y = 0; velocity.y = 0;
} }

@ -539,7 +539,8 @@ PlayerSettings:
allowUnsafeCode: 0 allowUnsafeCode: 0
additionalIl2CppArgs: additionalIl2CppArgs:
scriptingRuntimeVersion: 1 scriptingRuntimeVersion: 1
apiCompatibilityLevelPerPlatform: {} apiCompatibilityLevelPerPlatform:
Standalone: 3
m_RenderingPath: 1 m_RenderingPath: 1
m_MobileRenderingPath: 1 m_MobileRenderingPath: 1
metroPackageName: Template_3D metroPackageName: Template_3D

Loading…
Cancel
Save