Websocket for desktop, web and mobile [PC / MAC / Webplayer / WebGL / iOS / Android]

Hello guys I’m happy to announce the availability of WebSocket for desktop, web and mobile.

Here is the link: WebSocket for desktop, web and mobile | Network | Unity Asset Store

Soon I will upload a Lite version (without mobile implementation).

This package provides code to use client websocket with only one implementation for all platforms.

Currently it works on Desktop (PC/MAC), WebPlayer, WebGL (with Unity 5), iOS (device or simulator) and Android. I didn’t test on Linux but it should be the same than PC or Mac.

It does NOT require Unity Pro.

I used existing implementation for each platforms, so you can add or extend platforms if you want, for example you will be able to add Win8 platforms.

You can use this thread if you have questions or you can contact me directly : support@pavlou.fr.

Current functions on WebSocket object : Functions :

  • open connection
  • close connection
  • isOpen : test if the connection is open
  • send string message
  • send bytes data
  • open connection on secure webscoket : basic WSS connection : currently, I don’t manage parameters for the connection, only direct opening and it doesn’t work on Android, I will try to manage the direct connection on this platform fastly.

(Specific Unity 4.X, not necessary with Unity 5) : Currently the only manual step (only for free version of unity) is to rename/remove a dll when you switch platforms (on desktop and webplayer you need this dll, but you must not have it on ios or Android).
(You need to move some files in the Editor and Plugins folder the first time too).

A code sample, you can find the full demo file in the package : check TestWebSocketController.cs.

// first step : we need to create the WebSocket
//      You can use a websocketURL (ws://XXXXXXX or wss://XXXXXXXXX)
//        You need to give a MonoBehaviour object who will receive events
//        This object must extend WebSocketUnityDelegate interface
//      For example : TestWebSocketController inherits of WebSocketUnityDelegate
//        You can create your own websocket server
//        for example : https://github.com/willryan/em-websocket
// Warning : some libraries (for example on ios) don't support reconnection
//         so you need to create a new websocketunity before each connect (destroy your object when receive a disconnect or an error)
webSocket = new WebSocketUnity("ws://echo.websocket.org", this); // <= public server
//webSocket = new WebSocketUnity("ws://localhost:8080", this); // <= local server

// Second Step : we open the connection
webSocket.Open();

// Third Step : we need to close the connection when we finish
webSocket.Close();

// Fourth Step : we can send message
//    In this sample, we are waiting a "echo" message from the server
//    when we will receive this message, we will be able to change the display
webSocket.Send("Hello World");

// Fifth Step : we can send Data
//    In this sample, we are waiting a "echo" message from the server
//    when we will receive this message, we will be able to change the display
// You need a server which manages bytes message

int test1 = 42;
int test2 = 33;
byte[] data = new byte[8];

byte[] testB1 = System.BitConverter.GetBytes(test1);
byte[] testB2 = System.BitConverter.GetBytes(test2);

testB1.CopyTo(data,0);
testB2.CopyTo(data,4);

webSocket.Send(data);

After you need to implement the callbacks inherited of WebSocketUnityDelegate to manage all events :

    // Event when the connection has been opened
    void OnWebSocketUnityOpen(string sender);
  
    // Event when the connection has been closed
    void OnWebSocketUnityClose(string reason);
  
    // Event when the websocket receive a message
    void OnWebSocketUnityReceiveMessage(string message);
  
    // Event when the websocket receive data (on mobile : ios and android)
    // you need to decode it and call after the same callback than PC
    void OnWebSocketUnityReceiveDataOnMobile(string base64EncodedData);
  
    // Event when the websocket receive data
    void OnWebSocketUnityReceiveData(byte[] data);
  
    // Event when an error occurs
    void OnWebSocketUnityError(string error);

It doesn’t seem to work on unity5

Hello, I didn’t make the port for Unity 5. I will finish it asap, it has to be only a change of folder for some files. I will add the description of the process here to avoid the waiting of new version on the asset store (put I will submit too).

Is it working with Unity 5?

I did basic tests and it seems work (I need to test ios to be sure), you just need to make a settings linked to new Unity : you need to choose what will be the platform for all files in “plugins” folder :

  • File in Android is only for Android (select the file and select platform in Propriety window).
  • Files in iOS are only for IOS
  • DLL in x86_64 are for Editor/Webplayer/Mac/Linux/Windows

Don’t hesitate to say me if you have problems. I will upload the specific package for Unity 5 soon.

Thanks,
Jonathan

I submitted the package for Unity5, it will be available in few days (after the validation process).

Is the version with the Unity 5 fix available now?

The unity5 version is in the validation process, it can take 10days, I sent the package last week.

Sorry, the package is in the store since yesterday, so according to your version of Unity, you can retrieve the package 4.X or 5.X. Say me if you meet problems with these packages.

Hi, just downloaded the package from the store and having no luck. Currently running on 4.6.6f2. The release notes say it is compatible with 5 but I am assuming it works with 4 also. Help?

Hello, there are two versions in the Asset Store : 5.0 and 4.6.1 but I don’t know how Unity manages the versionning system, according to me if you went on the asset store with a 4.6.X version, you would download 4.6 version but it seems not work ? Can you send me (on my support mail) your package, I will send you the good one by mail.

Does this work with WebGL?

I never tested with WebGL, I will try this week end.

1 Like

Hi Sephiroth74,

I will send you the package I received, I managed to get the ios version working but not the android, so hopefully the replacement will sort this out. Thanks for your help in the meantime

For tracking : we continued by mail and the package works on android. Don’t hesitate to send me an email if you have other questions. I didn’t have the time to test WebGL, I will try to test tomorrow, I will update this thread after :). Thanks.

1 Like

Hello I tested, and it doesn’t work with WebGL, I need to rebuild a lib, I will do the changes soon, I will update the code to be able to use Editor with ios and Android selectionned platform too.

Thanks for testing, tell me when it’s fixed so I can purchase it expecting 0 problems!

Hello, I added WebGL support ( only on Unity 5, the code should work on Unity 4 but it will need manuel steps before build so I won’t update Unity 4 package). You will be able to work in Editor when you select android, ios or WebGL build, I use now the desktop implementation for these cases.

I submited the package this morning so it will be available in few days.

I wont to connect wss://echo.websocket.org and get this message WebSocket Error : An exception has occurred while connecting.