0

I am writing an application where I need to throw information using websockets. I came across the websocket-sharp library. As given in the guide, I have put the .dll file inside the Assets/Plugins/ location. However, often it happens that after a restart of my system of unity, it starts throwing error the Websocket sharp namespace or directive could not be found. I have to reimport or copy and paste. Also some of the related scripts or resources based on Websockt sharp like unityros, while building with .NET backend scripting fails to build throwing errors for includes like:

using System.Threading
using WebsocketSharp

a fix I found was to use:

#if UNITY_EDITOR
   using System.Threading
   using WebsocketSharp
#endif

and also need to add functions within the same if and endif conditions that use those include headers (sorry I am a C++ language guy - apologies for the terminologies like include headers). But no error is thrown when I build these projects with setting backend scripting to IL2CPP from the Unity Player Settings.

Can I use an IL2CPP file for deploying it in MR devices like hololens? Build configuration for MR devices would be Release, x64, Remote Device. Kindly advise.

1 Answers1

1

The problem you described in your question has nothing to do with using IL2CPP or .NET as backend.

The biggest issue is that System.Threading is not supported on HoloLens. HoloLens uses UWP which has many .NET API stripped out so you must use UNITY_EDITOR directive to remove any code that uses it when building for platforms that do not support.

Use MessageWebSocket when building for HoloLens and the websocket-sharp for other platform. See MessageWebSocket github for many examples on how to use that.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I was thinking the same, it was kinda naive question thinking about the scripting backend. But surprisingly, when i change .NET to IL2CPP everything builds fine with out UNITY_EDITOR (if and endif). Just been wondering why? – blackmamba Jan 03 '18 at 03:26
  • Did you run it? Does it **work** with IL2CPP on HoloLens ? I don't mean compile...I mean actually running like it does in the Editor – Programmer Jan 03 '18 at 03:30
  • I realized I cannot have it deployed to hololens. So went ahead with the directive UNITY_EDITOR, still testing websocketsharp. It works one moment and it doesnt work another moment. – blackmamba Jan 03 '18 at 07:26
  • *"It works one moment and it doesn't work another moment."*. No, not true. It clearly says "Not available for such UWP" on the github page for that. You have to use MessageWebSocket. That's only possible if Unity went around and implemented those missing API for IL2CPP. – Programmer Jan 03 '18 at 07:33
  • Thanks @Programmer, I am trying to do the websocket connection as a response to a click on my connect button. Any pointers? – blackmamba Jan 03 '18 at 08:42
  • Note that if your question is answered and solved, you should [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) the answer. If you want to do other things that's not the main question, you must create and ask a new question. – Programmer Jan 04 '18 at 01:19
  • You can use these APIs on HoloLens if you use IL2CPP. Unity uses full .NET API surface on IL2CPP as opposed to .NET (where you can only use what Microsoft approved). – Sunius May 15 '18 at 00:48
  • @Sunius Interesting. It compiled in the past but didn't work few years ago when I tried this with IL2CPP I don't know if this has changed. Even OP said he [cannot](https://stackoverflow.com/questions/48070457/which-backend-scripting-build-is-required-for-websocket-sharp-il2cpp-or-net/48070571?noredirect=1#comment83119460_48070571) have it deployed to Hololens – Programmer May 15 '18 at 01:11
  • This changed with introduction of IL2CPP scripting backend. I don't know what the OP meant that he cannot deploy it - it works on my side. – Sunius May 15 '18 at 15:16
  • Then it's very likely that something changed with IL2CPP in the recent update. What's your Unity version? I will update this answer when I have time to do test my self – Programmer May 15 '18 at 15:42