Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HttpListener and Websockets

Discussion in 'Editor & General Support' started by ZeBronca, Dec 17, 2020.

  1. ZeBronca

    ZeBronca

    Joined:
    Aug 22, 2012
    Posts:
    3
    Hi All,

    I have been trying to build a WebSocket server in Unity using the HttpListener API. However, it looks like this feature is not implemented by Unity. Calling IsWebSocketRequest always returns false, and the method AcceptWebSocketAsync throws a NotImplemented exception. The same code works when running outside Unity though.

    So, I just would like to confirm if Unity implements those APIs or not. And if they are not implemented, is there any plan to have it implemented in future releases?

    I have been testing those APIs in Unity Editor 2019.4.7.
     
  2. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    Were you ever able to resolve this issue? I've got a WS server that needs to be ported into unity and have just hit this issue after setting all my dependencies up.
     
  3. TheNecroposter

    TheNecroposter

    Joined:
    Aug 19, 2023
    Posts:
    1
    bump, having the same problem I need answers!
     
  4. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    As far as I'm concerned WebSockets in Unity/Mono is a total dead end (~300 hours of productivity lost, who at Unity/Mono do I invoice?).

    There are too many missing functionalities that are essentially empty method signatures for the sake of the compiler not flagging missing interface exceptions. WebSocketSharp alleviates the implementation issues for the most part, but it has it's limitations too.

    To make matters worse, after your WS: prototype is running, you'll inevitable try protect your WebSocket (or WS#) server with SSL certificate encryption. At this point after many hours have already been spent, you'll encounter missing functionality surrounding the various X509Certificate2 constructors, as well as limited certificate keystore support. Basically, instead of your app using the system keystore as is best practice, Unity/Mono want you to embed your certificate & keys into the build itself or load the certificate from file. (Fine for public key encryption, not for private key decryption.)

    Even using a C++ Interop .dll to retrieve the certificate as a pointer or raw byte[] data and pass it back to Unity runs into limitations and constructor support issues.

    As of this week, I've pivoted to having unity launch a separate headless server application (windows builds, sorry) to which the hosting Unity app connects using a 'host' role and the other apps connect as 'joiner' roles. This has it's own pros and cons, but is the 8th option I'm trying...

    Obligatory: Don't try to roll your own X509Certificate, encipherment, or SSL functionality it takes a whole lot of knowledge & testing to do it even half assed, let alone securely.
     
    Kurt-Dekker likes this.