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

Question Websocket Server in Standalone build

Discussion in 'Multiplayer' started by Reahreic, Mar 10, 2021.

  1. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    I'm merging my existing standalone WebSocket Server into a unity project in order to allow the Unity project to accept communications from a webpage that's delivered via a learning management system but have run into an Issue I don't know how to resolve.

    The existing WebSocket server operates perfectly as a .Net 4.5.2 application using System.Net's HTTPListener and WebSockets Classes, however when I merge the code into a Unity Standalone Build, I encounter two Issues.

    (Minor) Issue 1:
    HttpListenerContext.Request.IsWebSocketRequest
    is always false. Even when the request is correctly formed. I've worked around this by manually evaluating the request and sending an appropriate 'upgrade' response that's accepted by the client.

    (Major) Issue 2:
    [quote]H̶t̶t̶p̶L̶i̶s̶t̶e̶n̶e̶r̶.̶G̶e̶t̶C̶o̶n̶t̶e̶x̶t̶A̶s̶y̶n̶c̶(̶)̶ HttpListenerContext.AcceptWebSocketAsync()
    is not supported within unity! I've looked into manually implementing this, but it's not a quick or easy task and roadblocks forward momentum.

    As I seldom need to network or support communication within the applications I build, I'm not sure whether there's a unity mandated, or idiosyncratic implementation that must be followed instead and am open to advice on the matter.

    Edit: Copied the wrong line for issue 2, fixed.
     
    Last edited: Mar 12, 2021
  2. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    Almost a full working day with no views let alone comments, @CreativeChris, @AskCarol, @luke-unity Is there a better place for me to ask this question?
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Last edited: Mar 11, 2021
  4. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    My apologies, my brain dead self copied the wrong line into the original post, I meant
    HttpListenerContext.AcceptWebSocketAsync()

    Specifically, when using the result of the afore mentioned GetContextAsync to get the WebSocket Context
    HttpListenerWebSocketContext wsContext = await httpContext.AcceptWebSocketAsync(subProtocol: null);
    A 'Not Implemented Exception' is thrown.

    That said, it may be a mute point now as I trashed the entire class yesterday after coming across the ported WebSocketSharp library on GitHub late yesterday. I've been implementing it as unity specific alternate, and while It's not perfect, I have at lease been able to accept connections again.
     
    Joe-Censored likes this.
  5. holo-krzysztof

    holo-krzysztof

    Joined:
    Apr 5, 2017
    Posts:
    77
  6. KurtGokhan

    KurtGokhan

    Joined:
    Jan 16, 2013
    Posts:
    37
    @Reahreic Can you post the code you used to solve the first issue. I am having the same issue here and crying 6am in the morning. If it does not work, I will switch to WebSocketSharp because I know it works well with Unity.

    Btw, lol. Great trolling Unity. At least throw an exception. https://github.com/Unity-Technologi...System/System.Net/HttpListenerRequest.cs#L572

    Edit: Nevermind. I started using WebSocketSharp. It works well in Unity. The problem is, it does not work in .Net Core. So you cannot have a all-platform solution in one.
     
    Last edited: Mar 23, 2021
  7. holo-krzysztof

    holo-krzysztof

    Joined:
    Apr 5, 2017
    Posts:
    77
  8. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
  9. holo-krzysztof

    holo-krzysztof

    Joined:
    Apr 5, 2017
    Posts:
    77
    I don't know.

    It's not fixed, that's for sure.