AirConsole / airconsole-unity-plugin

A plugin for Unity to develop for AirConsole
http://developers.airconsole.com/#/guides/unity
GNU Lesser General Public License v3.0
69 stars 19 forks source link

Web Server initialization not working as intended and leads to exceptions #65

Open bbeinder opened 3 months ago

bbeinder commented 3 months ago

While debugging my game I ran into situations where I got a SocketException: Address already in use on the HttpListener indicating that the thread running it wasn't stopped correctly (which it wasn't, I checked). After that I had to restart Unity or terminate the process blocking the port manually. The error is not easy to reproduce but I had a look at the implementation and found another problem.

Due to the InitializeOnLoad the class Extentions gets reinitialized on every domain reload (every time the editor enters play mode or the script is reloaded). This also reinitializes all static variables including the webServer variable. Therfore the "Stop" and "Restart" button as well as "is running" state in the AirConsole settings become pretty useless as the server is restarted (or more specifically a new server is started) on entering the play mode anyway.

In order for it to work as intended one would probably have to disable Domain Reloading, but that has other implications and is not disabled by default.

Removing the webserver.Start() call from the Extentions constructor solved the original problem (as far as I've experienced) and another NullReferenceException that is thrown when a request is made to the HttpListener before the startupPath was set. The later can happen when exiting and entering playmode in rapid succession.