Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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:
    150
    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.