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

@ -17,15 +17,22 @@ public class Networking : ScriptableObject {
public int port;
private ClientWebSocket sock;
private Task connectTask;
private Task writeTask;
private Task<WebSocketReceiveResult> readTask;
private ArraySegment<byte> readBuffer;
private int seq;
public void Connect() {
if (isConnected()) {
return;
async public void Connect() {
if (sock != null) {
if (sock.State == WebSocketState.Open) {
return;
}
sock = null;
}
readBuffer = new ArraySegment<byte>(new byte[32000]);
Application.quitting += autoDisconnect;
sock = new ClientWebSocket();
UriBuilder b = new UriBuilder();
b.Scheme = "ws";
@ -33,10 +40,8 @@ public class Networking : ScriptableObject {
b.Port = port;
b.Path = path;
Debug.LogFormat("Connecting to: {0}", b.Uri);
connectTask = sock.ConnectAsync(b.Uri, CancellationToken.None);
connectTask.ContinueWith((x) => {
Debug.LogFormat("Finished connection task with status: {0}", sock.State);
});
await sock.ConnectAsync(b.Uri, CancellationToken.None);
Debug.LogFormat("Finished connection task with status: {0}", sock.State);
}
public void SendCollectSoul(string playerName, Vector3 position) {
@ -74,8 +79,24 @@ public class Networking : ScriptableObject {
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() {
if (isConnected()) {
Debug.Log("disconnecting websocket via autoDisconnect");
Task task = sock.CloseAsync(WebSocketCloseStatus.NormalClosure, "closed", CancellationToken.None);
task.Wait();
}

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

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

Loading…
Cancel
Save