void ResponseThread() { while (true) { WebRequest request = WebRequest.Create("http://localhost:5000"); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; request.ContentType = "text/json"; request.Method = "POST"; PayloadFromClient fromClient = new PayloadFromClient(); fromClient.commands = simulator.commands; byte[] binary = Encoding.UTF8.GetBytes(JsonUtility.ToJson(fromClient)); //manager.SetCommandSent(); server does this using (Stream postStream = request.GetRequestStream()) { // Send the data. postStream.Write(binary, 0, binary.Length); postStream.Close(); } simulator.SetCommandsSent(); // Get the response. WebResponse response = request.GetResponse(); // Display the status. Debug.Log(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. // The using block ensures the stream is automatically closed. using (Stream dataStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); Debug.Log(responseFromServer); PayloadFromHost data = JsonUtility.FromJson <PayloadFromHost>(responseFromServer); if (data == null) { Debug.Log("null"); } else { simulator.units = data.units; } } // Close the response. response.Close(); } }
void ResponseThread() { while (true) { HttpListenerContext context = _httpListener.GetContext(); // get a context /* // Now, you'll find the request URL in context.Request.Url * byte[] _responseArray = Encoding.UTF8.GetBytes("<html><head><title>Localhost server -- port 5000</title></head>" + * "<body>Welcome to the <strong>Localhost server</strong> -- <em>port 5000!</em></body></html>"); // get the bytes to response * //XmlSerializer ser = new XmlSerializer(typeof(List<Unit>)); */ //BinaryFormatter binaryFormatter = new BinaryFormatter(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(context.Request.InputStream); //BattleData data = (BattleData)binaryFormatter.Deserialize(context.Request.InputStream); string resposeFromClient = reader.ReadToEnd(); // Debug.Log(resposeFromClient); try { PayloadFromClient fromClient = JsonUtility.FromJson <PayloadFromClient>(resposeFromClient); List <Command> commands = new List <Command>(); Debug.Log(resposeFromClient); for (int i = 0; i < fromClient.commands.Count; i++) { if (!fromClient.commands[i].sent) { commands.Add(fromClient.commands[i]); } else { //Debug.Log("wrong command"); } } simulator.commands.AddRange(commands); } catch { Debug.Log("data error"); } //byte[] _responseArray = Encoding.UTF8.GetBytes(JsonUtility.ToJson(data)); PayloadFromHost payload = new PayloadFromHost(); payload.units = simulator.units; byte[] _responseArray = Encoding.UTF8.GetBytes(JsonUtility.ToJson(payload)); //context.Response.ContentLength64 = _responseArray.LongLength; context.Response.OutputStream.Write(_responseArray, 0, _responseArray.Length); // write bytes to the output stream context.Response.Close(); // close the connection Debug.Log("Respone given to a request."); } }