-1

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?

P.S. I saw some native C# frameworks for WebSocket, such as WebSocket-Sharp, but I'm afraid if there are any restrictions and will not work under Unity WebGL exports?

gman
  • 100,619
  • 31
  • 269
  • 393
Martin Asenov
  • 1,288
  • 2
  • 20
  • 38

2 Answers2

1

Anything from the System.Net namespace or System.Net.Sockets is not supported on WebGL and this is for security reasons

Unity's uNET supports WebGL but it uses WebSockets protocol internally to do that.

HLAPI:

You can now connect with the client code.

LLAPI:

You can also listen to both WebGL and normal Unity protol with LLAPI:

NetworkTransport.AddWebsocketHost(topology, 8887, null);
NetworkTransport.AddHost(topology, 8888);

Not entirely sure if LLAPI is supported on WebGL. I don't see why its shouldn't so give that a try. HLAPI supports WebSocket on WebGL.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I think I have tried AddWebsocketHost and it didn't even reach a handshake. I will try it again. Actually adding host is quite confusing because all I want to do is create a Unity client to authoritative server. Also I have a very specific custom binary protocol so HLAPI is not very suitable for me – Martin Asenov Jul 14 '17 at 10:19
  • What's your Unity version? Please note that you should provide the port number to `HostTopology` when creating it's instance. [Here](http://www.jinqi166.com/2016/11/26/unity_unet_lowlevel_llapi/) is a complete server and client when using LLAPI with WebGL. Take a look at that and use it as your starting point. If that doesn't work, you need to use HLAPI or external library. – Programmer Jul 14 '17 at 11:25
  • thank you, I will look at that. How do you know that is exported under WebGL? is there any deployed version of that code? – Martin Asenov Jul 14 '17 at 13:36
  • Remember that you have not mentioned your Unity version. I hope that you are using the latest version. If not please update your version. The link I provided says that the code supports WebGL. – Programmer Jul 14 '17 at 13:57
  • I'm using Unity 5.6.0f3 – Martin Asenov Jul 14 '17 at 15:54
  • Unity 2017 is now the latest version but 5.6 should work. Try what's in that link first. If that fails, then you must use HLAPI or make your own Javascript plugin that implements WebSockets but Unity has already done this for you. See their free [external library](https://www.assetstore.unity3d.com/en/#!/content/38367) made by Unity on the Asset store.You can always load it and extend/add more features to it. – Programmer Jul 14 '17 at 16:10
  • I'm trying it now. Looks good on initial impression. I will update you once I play around with it. – Martin Asenov Jul 15 '17 at 12:25
0

This is a bit old but I still wanted to update this since other people will come accross it.

It looks like you are trying NetworkTransport.AddWebSocketHost() on the client side which will not work for WebGL builds. WebGL builds cannot act as hosts... https://docs.unity3d.com/ScriptReference/Networking.NetworkTransport.AddWebsocketHost.html

You will need to use NetworkTransport.AddHost() like normal on the web client.

I got it working and sending messages to server works fine, but for some reason I can't receive messages from the server to the client. Web Debugger gave an error of... SerializationException: Unexpected binary element: 0

So now I am chasing this one down. :)

jwlewis
  • 21
  • 1
  • 4
  • Did you ever get WebGL to work with LLAPI? https://forum.unity.com/threads/llapi-with-webgl-addwebsockethost.526206/ – user7431543 Apr 11 '18 at 04:02
  • I eventually got my WebGL chat working in a browser on desktop and mobile but because it was so narly and the mobile browser version was so user UN-friendly I decided to go with downloadable apps for the pc and mobile. – jwlewis Apr 15 '18 at 20:47