Unity SocketIo using Best http2 plugin want to use in webgl

46 Views Asked by At

When ever I try to emit data using this best http2 plugin it emit data as a jsonstring but in backend side they want as a jsonobject and how many times i try to change it as a jsonOject using json.net and many other ways it still going as a jsonstring. If it is not possible then can any one tell me how i can use socket in webgl in unity. If any possibilities please inform me

I am trying to use socket in webgl. I use unity socket IO and best http2 socket IO but still facing so many issues in webgl for emitting and receiving data. If any way and answer possible please inform me. I can't use web socket i only have to use socket version 2

1

There are 1 best solutions below

2
Ipeleng Floyd Bela On

It seems you’re facing two main issues:

  1. Emitting data as a JsonObject instead of a JsonString using the Best HTTP/2 plugin in Unity.
  2. Using Socket.IO in WebGL with Unity.

Emitting Data as JsonObject The Best HTTP/2 plugin might be serializing your data into a JSON string by default. You could try creating a custom encoder1 to handle the serialization process according to your needs. However, it’s important to note that the backend should be able to parse the JSON string into a JSON object.

Using Socket.IO in WebGL with Unity WebGL doesn’t allow direct access to IP Sockets due to browser security limitations2. However, you can use WebSockets or WebRTC (the two most common networking protocols supported by browsers) to get around this3. Unity doesn’t have a built-in API that allows you to use WebSockets or WebRTC, but you can use a JavaScript plugin to implement this245.

Here are some steps you can follow to set up Socket.IO with WebGL in Unity2:

Create a ‘Plugins’ folder in your Unity Assets directory if it is not already there. Create a new file called socket_client.jslib in the Plugins folder. This .jslib file is your browser script. Modify the contents of your script according to your needs. Consult relevant Unity Scripting Docs and SocketIO Docs. Remember, after building your project to WebGL, you will need to install socket.io and make the Unity instance accessible

Here is a Code example

  1. Emitting Data as JsonObject If you’re using Newtonsoft’s Json.NET, you can convert a string to a JObject like this:

C#

using Newtonsoft.Json.Linq;

string jsonString = "{ \"key\": \"value\" }";
JObject jsonObject = JObject.Parse(jsonString);
  1. Using Socket.IO in WebGL with Unity Here’s an example of how you might set up a .jslib file for Socket.IO:

Javascript

mergeInto(LibraryManager.library, {
  Socket_Connect: function () {
    if (typeof io !== 'undefined') {
      this.socket = io('http://localhost:3000');
    } else {
      console.error('Socket.IO not loaded');
    }
  },

  Socket_Emit: function (eventName, data) {
    if (this.socket) {
      this.socket.emit(Pointer_stringify(eventName), Pointer_stringify(data));
    }
  },

  Socket_On: function (eventName) {
    if (this.socket) {
      this.socket.on(Pointer_stringify(eventName), function (data) {
        // Handle data
      });
    }
  }
});

In your Unity C# script, you would then use [DllImport("__Internal")] to call these functions:

using System.Runtime.InteropServices;

public class SocketIO : MonoBehaviour {
  [DllImport("__Internal")]
  private static extern void Socket_Connect();

  [DllImport("__Internal")]
  private static extern void Socket_Emit(string eventName, string data);

  [DllImport("__Internal")]
  private static extern void Socket_On(string eventName);
}

Please feel free to ask any questions, I hope this helps </> Code By Ipeleng Floyd Bela