HTTP Request Issue - Lag

I heard you guys liked obscure problems :wink:

I’m currently experimenting with controlling a Philips Hue bulb from Unity but I am seeing a lot of lag that I can’t explain. I am using the UnityHTTP plugin and the following C# code:

using UnityEngine;
using System.Collections;
using SimpleJSON;

public Hashtable lightSettings = new Hashtable();

void Start () {
        myUrl = "http://" + <bridge url> + "/api/newdeveloper/lights/" + <light number> + "/state"; // where am I accessing it?
	}

	void Update () {
		if (Input.GetKeyDown (KeyCode.X))
		{
			updateMyLight();
		}
	}

	public void updateMyLight() {
		updateHashtable();
		sendToLight();
	}

	public void sendToLight() {
		HTTP.Request theRequest = new HTTP.Request( "PUT", myUrl, lightSettings ); // build the request

		theRequest.Send(); // send the request
	}


	public void updateHashtable() {
		lightSettings.Clear();
		lightSettings.Add("transitiontime", 1);
		lightSettings.Add("bri", 100);
		lightSettings.Add("sat", 0);
		lightSettings.Add("hue", 50000); 
		lightSettings.Add("on", true); 
	}

Using this code I can change the properties of the light, however there appears to be a 1+ second delay between Unity sending the message and the light reflecting these changes. I do not see this delay when I control the bulb via Philips’ own browser-based debug tool.

Unfortunately I am fairly new to both Unity and network coding. Is there something obvious I’m missing? Perhaps a series of checks Unity automatically performs?

I’ve been stuck for 48 hours now. Any tips would be much appreciated!

Fixed this (with the help of a friend), it was actually some code in the UnityHTTP scripts. In the Request.cs file we quoted out the following lines:

response = new Response ();
response.request = this;
state = RequestState.Reading;
response.ReadFromStream(ostream);

and

switch (response.status) {
case 307:
case 302:
case 301:
uri = new Uri (response.GetHeader ("Location"));
continue;
default:
retry = maximumRetryCount;
break;
}

And now the Hue bulb responds almost immediately.