Headaches, Headaches, and more Headaches
After trying to open up the socket connection and send data back to the Unity code I quickly ran into… Headaches. First whatever package Mozilla used in their example completely blocked me out from using sockets while their code was running so I couldn’t actually send data back once the server had started. I also couldn’t put their code into a thread because I got an odd error related to the package that I couldn’t find a solution too. These headaches led to a five-hour-long goose chase through their code.
I started by researching the python package that they sued called Tornado. Tornado is a package kind of like Flask where you use it to create sockets as well as an HTTP server. However, it is much more robust than flask in that every operation coming in from clients is threaded meaning it can process many more requests in the same amount of time as Flask and this allows it to handle up to thousands of clients. Read more about it here. So the first problem I think I found with this package is it doesn’t allow threading because it itself is a thread manager and needs to be run in the main class. The entire server runs in an asynchronous loop. The one thing that I found after five hours, however, is that because tornado is being used that means that the gateway communicates with the python-web things code using HTTP Requests! I drafted up some quick python code to test this theory and sure enough, if I navigated to the written page, in this case, 127.0.0.1/0/properties/on I could pull data about the current state of the device, and if I updated the state from the gateway I would see the change in the python code. Finally, to update these values the gateway uses HTTP put requests which basically replaces all the content at a certain URL with whatever String you pass in. I tried this in python too and it worked I created a flickering light!
Now I can use web requests from the unity code to change and read these values without even interacting with the python-webthings code. I can just pull the JSON data and decode it on Unity’s side. There is a built-in package for encoding and decoding JSON data. You can create a class like this and use Unity’s function described here to either assign values to the class variables and create JSON data or to read JSON data given the provided class structure matches.
[Serializable] public class
MyClass {
public int level;
public float timeElapsed;
public string playerName; }
I wrote the code for Unity that made the web requests and decoded the JSON and got it working… sort of. My first test was getting it to run once and sure enough, I got the value perfect! Then I tried putting it in the Unity while loop and uhhhh, I think I Ddosed my own laptop. My python server was no longer working and as for Unity I tried restarting the server and running the code again and it gave me a very weird error every time it tried to make a web request. Yeah so my fantastic idea of sending 60 HTTP request a second was terrible and I immediately moved on from it just to follow an equally bad idea.
I spent the next two hours trying to get the java code they provided to compile. I know Java is a much more robust language than python and it can handle multiprocessing a lot better so I thought it was worth a try. I did get it to compile and run but the process of getting the project downloaded, using maven to build it, and then tracking down the missing JAR files was so frustrating that I was just going to figure out how to do it in python.
After sifting through their python code the first time I remembered that they are running a weird tornado.ioloop to have tornado handle a background process, in this case I can use my Unity comms code and run it in the ioloop. I still can’t start or stop the server but that’s tomorrow problem.