Commit 59a69e89 authored by Sebastian Höffner's avatar Sebastian Höffner
Browse files

Moving communication into subdir

parent 4f15a04a
fileFormatVersion: 2
guid: e2bd6ffe55adfc34b8b3b28fadff6e95
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
using SimpleJSON;
namespace dlu
{
public abstract class Communication : MonoBehaviour
{
public SceneController sceneController;
void Awake()
{
if (sceneController == null)
{
sceneController = GetComponent<SceneController>();
}
StartServer();
}
protected abstract void StartServer();
protected void HandleRequest(JSONObject requestObject)
{
string command = requestObject["Command"];
Debug.Log($"[Communication] Command: '{command}'");
switch(command)
{
case "RESET_SCENE":
StartCoroutine(sceneController.ResetScene(ReplyToRequest));
break;
case "GET_CONTEXT":
StartCoroutine(sceneController.GetContext(ReplyToRequest));
break;
case "GET_OCCUPANCY":
StartCoroutine(sceneController.GenerateOccupancyGrid(float.Parse(requestObject["resolution"]), requestObject["exclude"] as JSONArray, ReplyToRequest));
break;
case "SIMULATE_SCENE":
StartCoroutine(sceneController.RunSimulation(requestObject["instructions"] as JSONArray, ReplyToRequest));
break;
default:
StartCoroutine(ReplyToRequest("{\"Error\": \"Server does not understand this command.\"}"));
break;
}
}
public abstract IEnumerator ReplyToRequest(string response);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 429a7dfc7a7974ccb8f8925740a5b392
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -11,7 +11,7 @@ namespace dlu
public class WebsocketCommunication : Communication
{
private WebSocket ws;
public string address = "ws://127.0.0.1:8392";
public string address = "ws://127.0.0.1:5000";
private bool workingOnRequest = false;
protected override void StartServer()
......@@ -23,12 +23,18 @@ public class WebsocketCommunication : Communication
ws.OnOpen += () =>
{
Debug.Log($"[WebSocket] Connected to {address}");
ws.Send(Encoding.UTF8.GetBytes("Hello"));
};
ws.OnMessage += ReceiveMessage;
ws.OnError += (string errMsg) =>
{
Debug.LogError("[WebSocket] error: " + errMsg);
};
ws.OnClose += (WebSocketCloseCode code) =>
{
Debug.Log($"[WebSocket] Disconnected from {address} (Reason: {code})");
ws = null;
};
ws.Connect();
}
......@@ -71,10 +77,15 @@ public class WebsocketCommunication : Communication
}
}
public void Send(string message)
{
Debug.Log($"[WebSocket] Sending message:\n{message}");
ws.Send(Encoding.UTF8.GetBytes(message));
}
public override IEnumerator ReplyToRequest(string response)
{
Debug.Log($"[WebSocket] Sending message:\n{response}");
ws.Send(Encoding.UTF8.GetBytes(response));
Send(response);
yield return null;
workingOnRequest = false;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment