Search Unity

Is websocket server ready to use?

Discussion in 'Unity Transport' started by rtytewfdfgfhfgkuio, Oct 14, 2022.

  1. rtytewfdfgfhfgkuio

    rtytewfdfgfhfgkuio

    Joined:
    Mar 3, 2022
    Posts:
    2
    I found WebsocketLayer in the package,but I don't kown how to use it to build websocket server,any sample code available to see?
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    We don't have any sample code for WebSocket support yet (documentation for the 2.0 release of the package is still being worked on). But the process to start a server is the same as for UDP-based connections, with the only difference that the network driver must be created with a WebSocket interface:
    Code (CSharp):
    1. var driver = NetworkDriver.Create(new WebSocketNetworkInterface());
    2. driver.Bind(NetworkEndpoint.AnyIpv4.WithPort(7777));
    3. driver.Listen();
    If you are using Netcode for GameObjects, you can also try out the development branch, which contains basic WebSocket support if version 2.0 of the transport package is installed (you'll need to force the update since by default the package will install 1.X).

    Note however that WebSocket support in the Unity Transport package is only meant to be used for communications between peers using the package. It is not a general-purpose WebSocket library. That is, if you create a WebSocket server using Unity Transport, only clients that are also using Unity Transport will be able to communicate with it.
     
  3. leeprobert

    leeprobert

    Joined:
    Feb 12, 2015
    Posts:
    49
    I am using Transport 2.1.0 and the `WebSocketNetworkInterface` is not found despite being in the documentation as it is above. I am trying to communicate with a streaming server that runs a localhost server on port 7788 so I assume the client code should work as-is. I just can't get it to set up the driver as a web socket. Any ideas?
     
  4. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Is it just the
    WebSocketNetworkInterface
    type that is not found or do you see the same issue for other types provided by the package (like
    NetworkDriver
    )? What exactly is the error message that you get?

    Also as written above, WebSocket support in Unity Transport is only meant to be used for communications between peers using the Unity Transport package. Communications with other types of peers (like servers not using Unity Transport) is not supported and will not work.
     
  5. leeprobert

    leeprobert

    Joined:
    Feb 12, 2015
    Posts:
    49
  6. leeprobert

    leeprobert

    Joined:
    Feb 12, 2015
    Posts:
    49
    I discovered that if I use the scripts from the samples folder as-is it doesn't show any errors for the `WebSocketNetworkInterface` ... is there another way to create a Websocket and send a message to this Vagon server that is hosting my app?
     
  7. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Yes. Unity Transport is not a general-purpose WebSocket library that can be used with any third-party WebSocket services (like Vagon). Even if you did get the WebSocket network interface to work, it would not communicate properly with that service.
     
  8. RageAgainstThePixel

    RageAgainstThePixel

    Joined:
    Mar 11, 2020
    Posts:
    66
    That's too bad, it would have been very useful.