Search Unity

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

Discussion in 'Assets and Asset Store' started by Sephiroth74, Jan 11, 2015.

  1. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Hello guys I'm happy to announce the availability of WebSocket for desktop, web and mobile.

    Here is the link: http://u3d.as/aYE

    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).
     
    Last edited: Jan 30, 2016
  2. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    A code sample, you can find the full demo file in the package : check TestWebSocketController.cs.

    Code (CSharp):
    1.  
    2. // first step : we need to create the WebSocket
    3. //      You can use a websocketURL (ws://XXXXXXX or wss://XXXXXXXXX)
    4. //        You need to give a MonoBehaviour object who will receive events
    5. //        This object must extend WebSocketUnityDelegate interface
    6. //      For example : TestWebSocketController inherits of WebSocketUnityDelegate
    7. //        You can create your own websocket server
    8. //        for example : https://github.com/willryan/em-websocket
    9. // Warning : some libraries (for example on ios) don't support reconnection
    10. //         so you need to create a new websocketunity before each connect (destroy your object when receive a disconnect or an error)
    11. webSocket = new WebSocketUnity("ws://echo.websocket.org", this); // <= public server
    12. //webSocket = new WebSocketUnity("ws://localhost:8080", this); // <= local server
    13.  
    14. // Second Step : we open the connection
    15. webSocket.Open();
    16.  
    17. // Third Step : we need to close the connection when we finish
    18. webSocket.Close();
    19.  
    20. // Fourth Step : we can send message
    21. //    In this sample, we are waiting a "echo" message from the server
    22. //    when we will receive this message, we will be able to change the display
    23. webSocket.Send("Hello World");
    24.  
    25. // Fifth Step : we can send Data
    26. //    In this sample, we are waiting a "echo" message from the server
    27. //    when we will receive this message, we will be able to change the display
    28. // You need a server which manages bytes message
    29.  
    30. int test1 = 42;
    31. int test2 = 33;
    32. byte[] data = new byte[8];
    33.  
    34. byte[] testB1 = System.BitConverter.GetBytes(test1);
    35. byte[] testB2 = System.BitConverter.GetBytes(test2);
    36.  
    37. testB1.CopyTo(data,0);
    38. testB2.CopyTo(data,4);
    39.  
    40. webSocket.Send(data);
    41.  
    After you need to implement the callbacks inherited of WebSocketUnityDelegate to manage all events :

    Code (CSharp):
    1.     // Event when the connection has been opened
    2.     void OnWebSocketUnityOpen(string sender);
    3.  
    4.     // Event when the connection has been closed
    5.     void OnWebSocketUnityClose(string reason);
    6.  
    7.     // Event when the websocket receive a message
    8.     void OnWebSocketUnityReceiveMessage(string message);
    9.  
    10.     // Event when the websocket receive data (on mobile : ios and android)
    11.     // you need to decode it and call after the same callback than PC
    12.     void OnWebSocketUnityReceiveDataOnMobile(string base64EncodedData);
    13.  
    14.     // Event when the websocket receive data
    15.     void OnWebSocketUnityReceiveData(byte[] data);
    16.  
    17.     // Event when an error occurs
    18.     void OnWebSocketUnityError(string error);
    19.  
     
  3. leifenberg1

    leifenberg1

    Joined:
    Apr 18, 2015
    Posts:
    1
    It doesn't seem to work on unity5
     
  4. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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).
     
  5. Talok

    Talok

    Joined:
    Apr 20, 2014
    Posts:
    3
    Is it working with Unity 5?
     
  6. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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
     
  7. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    I submitted the package for Unity5, it will be available in few days (after the validation process).
     
  8. krmaicher

    krmaicher

    Joined:
    Oct 9, 2014
    Posts:
    4
    Is the version with the Unity 5 fix available now?
     
  9. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    The unity5 version is in the validation process, it can take 10days, I sent the package last week.
     
  10. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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.
     
  11. benassia

    benassia

    Joined:
    May 7, 2013
    Posts:
    2
    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?
     
  12. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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.
     
  13. MidgardDev

    MidgardDev

    Joined:
    Dec 11, 2012
    Posts:
    47
    Does this work with WebGL?
     
  14. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    I never tested with WebGL, I will try this week end.
     
    MidgardDev likes this.
  15. benassia

    benassia

    Joined:
    May 7, 2013
    Posts:
    2
    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
     
  16. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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.
     
    MidgardDev likes this.
  17. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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.
     
  18. MidgardDev

    MidgardDev

    Joined:
    Dec 11, 2012
    Posts:
    47
    Thanks for testing, tell me when it's fixed so I can purchase it expecting 0 problems!
     
  19. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    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.
     
  20. annaniks

    annaniks

    Joined:
    Feb 22, 2015
    Posts:
    2
    I wont to connect wss://echo.websocket.org and get this message WebSocket Error : An exception has occurred while connecting.
     
  21. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Sorry, I didn't see the notification for this post, what is your unity version and the platform where you get the problem ?
    I used wss://echo.websocket.org to do my tests, I will check if something changed.
     
  22. amlinux

    amlinux

    Joined:
    Apr 24, 2016
    Posts:
    5
    Hi Jonathan, is there any chance to have Android support for wss? If you don't have time to do it yourself, I will happily implement it myself and contribute back, if you don't mind of course.
     
    char_stiles likes this.
  23. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Hello, I won't have time these next weeks to do it. But if you want to do it, don't hesitate to send me an email (jonathan@pavlou.fr) and we will talk about it.
     
  24. Beregolas

    Beregolas

    Joined:
    May 4, 2016
    Posts:
    1
    Hi Jonathan,
    The Package is working fine for ws: connections, but I have the same Problem with wss:// Requests to wss://echo.websocket.org "WebSocket Error : An exception has occurred while connecting."
    I'm using it to prototype a client in WebGL (current Unity version, 5.3.4), and sadly we don't have our own server set up right now, so I cannot test against another wss-server.
     
  25. Rigor_Mortis

    Rigor_Mortis

    Joined:
    Jul 17, 2014
    Posts:
    2
    I came across this error when compiling for iOS with Unity 5:

    ld: warning: ignoring file /AppleProjects/xxxx/Libraries/WebSocketUnity/Plugins/iOS/libWebSocketUnity-iossimulator.a, missing required architecture armv7 in file /AppleProjects/xxxx/Libraries/WebSocketUnity/Plugins/iOS/libWebSocketUnity-iossimulator.a (2 slices)
    Undefined symbols for architecture armv7:
    "_utf8_nextCharSafeBody", referenced from:
    -[SRWebSocket _innerPumpScanner] in libWebSocketUnity-ios.a(SRWebSocket.o)

    Is there a fix for this ? I assume if I was compiling for only arm64 it would work fine but I want to support as many devices as possible.

    Thanks,

    Dave
     
  26. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Hello,

    Yes I need to rebuild my library and update the package. It takes time to validate a new package, so send me an email (support@pavlou.fr) and I will send you the ios library.

    Regards,
    Jonathan
     
  27. ereneld

    ereneld

    Joined:
    Sep 19, 2014
    Posts:
    1
  28. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Hello,

    No I don't support custom header, it's a good idea.
    With my package, I support WebGL, ios, android with native implementation.

    Jonathan
     
  29. BenBart

    BenBart

    Joined:
    Jun 21, 2013
    Posts:
    1
    I'm having trouble getting this to work on Windows 10. Any tips on how to include "Websocket"? It is just being commented out, because of the conditional include statements?
     
  30. Kalle-Cederstrom

    Kalle-Cederstrom

    Joined:
    Jan 22, 2014
    Posts:
    6
    How do I fix this in XCode 8?
    ld: warning: ignoring file /Users/UserName/Desktop/Projects/Project26/Libraries/Standard Assets/Scripts/WebSocketUnity/Plugins/iOS/libWebSocketUnity-iossimulator.a, missing required architecture arm64 in file /Users/UserName/Desktop/Projects/Project26/Libraries/Standard Assets/Scripts/WebSocketUnity/Plugins/iOS/libWebSocketUnity-iossimulator.a (2 slices)
    ld: '/Users/UserName/Desktop/Projects/Project26/Libraries/Standard Assets/Scripts/WebSocketUnity/Plugins/iOS/libWebSocketUnity-ios.a(WebSocketUnityInterface.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
     
  31. btiger19

    btiger19

    Joined:
    Jun 28, 2009
    Posts:
    56
    Hi
    I just use golang for server backend, I can use browser to connect to the server and can send message and get message by websocket. But in Unity, I can not connect to the server, it's always said:
    WebSocket Close : An error has occurred while connecting.
    UnityEngine.Debug:Log(Object)
    TestWebSocketController:OnWebSocketUnityClose(String) (at Assets/Demo/TestWebSocketController.cs:119)
    WebSocketUnityCSharp:OnDisconnected(Object, CloseEventArgs) (at Assets/WebSocketUnity/Platforms/WebSocketUnityCSharp.cs:72)
    WebSocketCSharp.Ext:Emit(EventHandler`1, Object, CloseEventArgs)
    WebSocketCSharp.WebSocket:close(PayloadData, Boolean, Boolean)
    WebSocketCSharp.WebSocket:close(CloseStatusCode, String, Boolean)
    WebSocketCSharp.WebSocket:doHandshake()
    WebSocketCSharp.WebSocket:connect()




    Here is the server code(golang):
    package main

    import (
    "fmt"
    "log"
    "net/http"

    "golang.org/x/net/websocket"
    )

    func Echo(ws *websocket.Conn) {
    var err error

    for {
    var reply string

    if err = websocket.Message.Receive(ws, &reply); err != nil {
    fmt.Println("Can't receive")
    break
    }

    fmt.Println("Received back from client: " + reply)

    msg := "Received: _________" + reply
    fmt.Println("Sending to client: " + msg)

    if err = websocket.Message.Send(ws, msg); err != nil {
    fmt.Println("Can't send")
    break
    }
    }
    }

    func main() {
    http.Handle("/", websocket.Handler(Echo))

    if err := http.ListenAndServe(":1235", nil); err != nil {
    log.Fatal("ListenAndServe:", err)
    }
    }



    I'm using ws://127.0.0.1:1235 for testing.
     
  32. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Hello,

    Can you send me an email (support@pavlou.fr), I will send you the source, you just need to rebuild it.
    Sorry for the inconvenience.

    Jonathan
     
  33. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi. So just to be clear, this is a client only, NOT a websocket server solution, correct?
     
  34. Sephiroth74

    Sephiroth74

    Joined:
    Dec 9, 2012
    Posts:
    20
    Hello, yes it's only a client implementation.

    Regards,
    Jonathan
     
  35. takoy

    takoy

    Joined:
    Jul 2, 2015
    Posts:
    20
    Hello Jonathan,

    I've been using your package for awhile now.
    Currently I am using it for an Android device and I am having issues with it.

    My app sends through the WebSocket a big chunk of data which causes it to crash/ get stuck.

    The reason for that is that when I use the WebSocket I have to use it through a MonoBehaviour object which implements the WebSocketUnityDelegate interface.
    In order to solve this issue I was thinking of using the WebSocket.Send inside a thread.

    The only problem with that is that I won't have callbacks from the native code since you are passing a gameobject name to the AndroidJavaObject:

    Code (CSharp):
    1. public WebSocketUnityAndroid(string url, string gameObjectName){
    2.         object[] parameters = new object[2];
    3.         parameters[0] = url;
    4.         parameters[1] = gameObjectName;
    5.         mWebSocket = new AndroidJavaObject("com.jonathanpavlou.WebSocketUnity", parameters);
    6.     }
    Is there a way to bypass it? Implement a different interface for it?

    Thanks,
    Tako
     
  36. takoy

    takoy

    Joined:
    Jul 2, 2015
    Posts:
    20
    Is there still support for this plugin???
     
  37. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Im trying to instantiate a gameobject when a message is received but I'm getting this error message:

    Assertion failed on expression: '!(!Thread::EqualsCurrentThreadID(GetPersistentManager().GetMainThreadID()) && mode != kCreateObjectFromNonMainThread)'

    Any thoughts?

    Thanks
     
  38. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    ok I got it working by storing all the messages in a List and then looping through the list in Update.
     
  39. GoEpik

    GoEpik

    Joined:
    Jan 22, 2018
    Posts:
    1
    Hello, I just purchased your package and I want to know if there is possible to use http protocol based server instead of ws or wss.

    Please, just let me know asap
     
  40. AlexHell

    AlexHell

    Joined:
    Oct 2, 2014
    Posts:
    167
    Hello! I also need to know: does it work on editor+webgl+android+ios (all of this) by WSS (secure)?
     
  41. jqian1590

    jqian1590

    Joined:
    Mar 23, 2016
    Posts:
    2
    build failed with Xcode 9 and IOS 11.4. There are some linker errors


    'WebSocketUnity/Plugins/iOS/libWebSocketUnity-ios.a(WebSocketUnityInterface.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64



    clang: error: linker command failed with exit code 1 (use -v to see invocation)




    Help!!!!!
     
  42. NorthStar79

    NorthStar79

    Joined:
    May 8, 2015
    Posts:
    88
    is this asset supported still? , I am considering to buy this but it is an old asset and I just want to be sure that it still works
     
    tonialatalo likes this.
  43. jbrasero

    jbrasero

    Joined:
    Aug 27, 2018
    Posts:
    2
    Hi,
    I have found the same problem that other member:

    ld: '/Users/jesusbrasero/Documents/GitHub/unity_ar_app/iOS/Libraries/WebSocketUnity/Plugins/iOS/libWebSocketUnity-ios.a(WebSocketUnityInterface.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    After disable bitcode, my error is following:

    Undefined symbols for architecture arm64:
    "_utf8_nextCharSafeBody", referenced from:
    -[SRWebSocket _innerPumpScanner] in libWebSocketUnity-ios.a(SRWebSocket.o)
    ld: symbol(s) not found for architecture arm64


    How can I fix it?

    Thanks in advance
     
  44. jbrasero

    jbrasero

    Joined:
    Aug 27, 2018
    Posts:
    2

    Hi!! Did you solve it?
    Thanks!!
     
  45. johanandre1976

    johanandre1976

    Joined:
    Jul 22, 2013
    Posts:
    1
    Hi!

    I seem to have some trouble with reconnecting if the socket server goes down and comes back online.
    What is the proper way to handle this?
     
    zeroui_tanuj likes this.
  46. zeroui_tanuj

    zeroui_tanuj

    Joined:
    May 19, 2018
    Posts:
    1
    I am having problems related to ios app getting frozen in the background on attempting WS connection when it's not available.