- Home /
Unity WebGL websocket LLAPI alternatives?
First of all I am deeply confused about HLAPI, LLAPI, and WebSocket support in Unity WebGL exports. HLAPI and LLAPI work over UDP, while WebSocket is TCP based and HLAPI and LLAPI claim to support it? Not sure how to feel about that.
I have an authoritative server written in Java. It accepts TCP connections. I had written the Unity client side with System.Net.Sockets. In the editor and for mobile exports it works fine, but when I tried to export for WebGL I saw it is not going to work.
Because I didn't want to make the server support two protocols (UDP and TCP), but unify it to use just TCP, I decided to keep what I've done so far in the client and use it for mobile exports, and write different logic to go with WebGL exports.
Unfortunately all UNET talks about is how to create client-side hosts and other client-side clients to connect with it, but it DOESN'T SAY A WORD how to use UNET to connect over WebSocket to authoritative server.
This is what I've tried and currently all it does is initiating a handshake:
override protected void initializeInternal ()
{
NetworkTransport.Init();
ConnectionConfig serverConfig = new ConnectionConfig ();
serverConfig.MaxCombinedReliableMessageCount = 1;
serverConfig.MaxCombinedReliableMessageSize = 1;
serverConfig.SendDelay = 0;
serverConfig.WebSocketReceiveBufferMaxSize = commandByteArraySize;
channelId = serverConfig.AddChannel(QosType.ReliableSequenced);
HostTopology topology = new HostTopology(serverConfig, 1);
hostId = NetworkTransport.AddHost (topology, port);
Connect ();
}
public void Connect() {
byte error;
connectionId = NetworkTransport.Connect(hostId, host, port, 0, out error);
Debug.Log("Connect host: " + host + "; port: " + port + "; error: " + (NetworkError) error);
}
override protected void writeData(byte[] data) {
byte error;
NetworkTransport.Send(hostId, connectionId, channelId, data, data.Length, out error);
Debug.Log("Send host: " + host + "; port: " + port + "; error: " + (NetworkError) error);
}
I didn't want to AddHost()
, but I had to, because otherwise it didn't seem to even try establish a connection to the authoritative server.
The server accepts the handshake over TCP (which I'm not sure why is that, the Unity WebGL export seems to do the handshake over TCP, while the standard says it should be done over HTTP), and responds with a proper handshake response.
When the game starts, writeData()
is called, but that data doesn't get to the server for whatever reason. The debug messages in the console say the status is Ok. However no messages received in the server.
All this, the lack of proper documentation on the topic, docs about LLAPI, HLAPI and bottom line how to use those libraries to initiate WebSocket communication under Unity WebGL exports boils down to one question:
With Unity WebGL exports, what good alternative libraries are there to establish connection to authoritative server which supports TCP?