Frequently Asked Questions
General issues
Does this asset support visual scripting?
No. This asset is made for developers working with code.
Why are we not creating Units for it?
Creating universal nodes for Socket.IO would cost lots of time and only target very few people. You can however create your own Visual Scripting Units for implementing Socket.IO communications. Implementing specialized units is much easier than creating universal nodes because our asset for example supports variable payload counts and types.
The game is not connecting to my server…
…not even locally (but the example works with the Firesplash Entertainment hosted demo server)
In most cases this is caused by one of the following reasons:
- You enabled the "Secure Connection" option in the component but did not correctly configure SSL on your server side – or vice versa! Try to access the Socket.IO server using a Webbrowser: http(s)://your.server-address.here/socket.io/ Don't forget the trailing "/" It should say {"code":0,"message":"Transport unknown"} If any browser errors appear, this is your issue. Be sure to use the exact same address as in the unity component at the red position, and set http for unchecked secure connect and https for checked.
- You are using the wrong port Socket.IO assumes port 80 (insecure) or 443 (secure). Make sure to enter the correct port if you are using some other. Test access like in solution1 to make sure everything is right
Now I was able to run a server locally and connect the game with it but… after uploading to the server it is no longer working again…
Well…
- Again: SSL might not be configured correctly (you won't communicate unencrypted in production, right?) Check SSL as done in the last question, possible solution 1. But this time using the actual server address.
- Your server does not accept the transport "websocket" or some layer in between drops the requests This is a whip of rocket science for most developers: Socket.IO for Unity depends on the "websocket" transport and is NOT able to fallback to long-polling. This is why a standard socket.io application might work while the unity asset is not working. Load Balancers, Reverse Proxies, Firewalls and any device/solution between the client and the server could potentially cause issues here. Usually the issue sits on the server side. Most likely – if using one – your load balancer or reverse proxy (like nginx/apache proxying requests through your servers port 80/443 down to a nodejs based server on a different port) is not configured correctly. Here are some hints for reverse proxy configuration: https://socket.io/docs/v3/reverse-proxy/
This Rocket-Science can easily be tested by writing a web based test application. Have a look at our example ServerCode zip archive. It contains such a test html file. The important thing about it is to only try the websocket transport using the options: https://socket.io/docs/v3/client-initialization/#transports
I am using a transport adapter on my server side…
Can I connect to this server using your asset?
Usually no. Transport adapters need to be implemented on both sides. You would have to rework our code to implement the transport adapter.
How do I Serialize/Deserialize complex objects?
Unity's builtin "JsonUtility" does not support complex objects. This means it only supports objects and only a single layer of complexity.
Job | Builtin JsonUtility variant / workaround | Better variant (requires Json.Net) |
---|---|---|
Employee data | { |
"firstname": "John", "surname": "Baker", "job": "Dentist" } | { name: { "first": "John", "last": "Baker" }, "job": "Dentist" } | | A list of numbers | { "a": 12, "b": 15, "c": 20} | [12, 15, 20] | | A dynamic list of employees | Not possible at all | [{ name: { "first": "John", "last": "Baker" }, "job": "Dentist" },{ name: { "first": "Martina", "last": "Carrey" }, "job": "Student" }, …] |
The good news: All those "bad" situations can be solved using Json.Net (see Instructions)
Callbacks (On) firing twice after a reconnect
When a reconnect happens, my listeners are invoked twice. After another reconnect three times, and so on. Why?
This is likely an issue in how - or better when - you are calling "On" to register your callbacks.
Registered Callbacks do survive a reconnect, as this is a client side registration. You must not register your callbacks in a "connect" event, except you add some kind of check that it is not being registered twice - or of course if this behaviour is wanted.
See also the official Socket.IO documentation.
How do I send request headers, for example to provide an oauth token?
Websockets - the underlaying technology of this socket.io implementation - do not support request headers (see also this stackoverflow discussion) so this is not directly possible.
Instead, socket.io implements it's own mechanism to send authentication data on connect time, using the method SocketIOClient.SetAuthPayloadCallback - There is an example in our provided example script. The Server-side API does not access a header, but accesses this data using a call like socket.handshake.auth.whatever
where whatever is the fieldname in your authentication payload.
This is the only officially supported way to provide data before the actual socket.io connection is established. Another (hacky) workaround could be to provide the token within the connect URI using query parameters.
See also the official Socket.IO documentation
WebGL / IL2CPP Build Issues
I am using WebGL (on an SSL-secured site)…
…and the game does not connect to the server.
The most common issue is an SSL-misconfiguration. Please make sure that you can access https:// <your-socketio-address> /socket.io/ from your browser without any SSL errors or warnings. (A json formatted transport error is fine) Also remember that in modern browsers, a WebGL game delivered via HTTPS may not contact a server using HTTP/WS but only using HTTPS/WSS so this means SSL everywhere or nowhere.
My IL2CPP-Build (WebGL is always IL2CPP) using Json.Net is showing errors…
…like Attempting to call method 'System.Collections.Generic.List[…]::.ctor' for which no AOT code was generated
For some reason using arrays in structures does not work on IL2CPP. Try to use Lists instead. struct Foo {public int[] bar;}could break, struct Foo {public List<int> bar;} works.
My WebGL (or other IL2CPP) build using Json.Net is crashing…
…telling me that a constructor or type has not been found
Most likely the linker stripped code that is used by Json.Net. You should add a link.xml file to your project which restricts the linker from doing so. Its content depends on your project but here is an example: https://github.com/SaladLab/Json.Net.Unity3D/blob/master/src/UnityPackage/Assets/link.xml
I imported Json.Net and now I am getting a lot of "duplicate assembly" errors
Sometime around mid of 2021 Unity started to deliver their "own" Json.Net package with some components. If your project already contains that package, installing Json.Net will cause this behavior. It is up to you to decide what to do. Unity officially states that their package is not meant to be used but on the other hand you got no real choice. Seen from a dev's perspective it should not cause any issues so if your project includes unity's version, remove your imported one and use unity's. https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@2.0/manual/index.html
Mobile Platforms
The asset should work on mobile platforms without any issues if you configure your project correctly.
Android build does not connect to the server
Please make sure, you set this to required in project settings: