Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

How to send a string to a websocket server using Unity Transport 2.1.0

Discussion in 'Unity Transport' started by leeprobert, Nov 13, 2023.

  1. leeprobert

    leeprobert

    Joined:
    Feb 12, 2015
    Posts:
    50
    I am using `Unity Transport` with a `WebSocket` to connect to a game streaming service called `Vagon`. In order to launch a URL from their streaming service you have to setup a client web socket and then send a message to request the URL launch from the browser. The JSON formatted plain text message is:
    ```
    Code (CSharp):
    1.  
    2. {
    3.   "$type": "8",
    4.   "Url": "https://www.vagon.io"
    5. }
    6.  
    ```
    I have the following setup to connect to their web socket server:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using Unity.Networking.Transport;
    4. namespace Unity.Networking.Transport.Samples
    5. {
    6.     public class WebSocketClientBehaviour : MonoBehaviour
    7.     {
    8.         NetworkDriver m_Driver;
    9.         NetworkConnection m_Connection;
    10.         private bool _shouldRequestUrlOpen = false;
    11.         private string _url = "https://travelvrse.com";
    12.         void Start()
    13.         {
    14.             m_Driver = NetworkDriver.Create(new WebSocketNetworkInterface());
    15.             var endpoint = NetworkEndpoint.LoopbackIpv4.WithPort(7788);
    16.             m_Connection = m_Driver.Connect(endpoint);
    17.         }
    18.         void OnDestroy()
    19.         {
    20.             m_Driver.Dispose();
    21.         }
    22.         void Update()
    23.         {
    24.             m_Driver.ScheduleUpdate().Complete();
    25.             if (!m_Connection.IsCreated)
    26.             {
    27.                 return;
    28.             }
    29.             Unity.Collections.DataStreamReader stream;
    30.             NetworkEvent.Type cmd;
    31.             while ((cmd = m_Connection.PopEvent(m_Driver, out stream)) != NetworkEvent.Type.Empty)
    32.             {
    33.                 if (cmd == NetworkEvent.Type.Connect)
    34.                 {
    35.                     Debug.Log("[WebSocket] We are now connected to the server.");
    36.                     uint value = 2;
    37.                     m_Driver.BeginSend(m_Connection, out var writer);
    38.                     writer.WriteUInt(value);
    39.                     m_Driver.EndSend(writer);
    40.                 }
    41.                 else if (cmd == NetworkEvent.Type.Data)
    42.                 {
    43.                     uint value = stream.ReadUInt();
    44.                     Debug.Log($"[WebSocket] Got the value {value} back from the server.");
    45.                     m_Connection.Disconnect(m_Driver);
    46.                     m_Connection = default;
    47.                 }
    48.                 else if (cmd == NetworkEvent.Type.Disconnect)
    49.                 {
    50.                     Debug.Log("[WebSocket] Client got disconnected from server.");
    51.                     m_Connection = default;
    52.                 }
    53.             }
    54.         }
    55.         public void RequestUrlOpen()
    56.         {
    57.             _shouldRequestUrlOpen = true;
    58.         }
    59.     }
    60. }
    So, as I understand it, I need to add the method to send the string. What I would like to know is whether I need to do this in the `update` method where the `writer` is currently sending the `UInt`? The call to send the URL is done through user initiation so I suspect that the `NetworkEvent.Type.Connect` event only happens once and therefore will not be suitable for sending the message.
    My question is, how do I create a method to write the message to the server?
     
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    As I indicated in the other thread, Unity Transport is not a general-purpose WebSocket library that can be used to communicate with WebSocket third-party services (like Vagon). It only supports communicating with peers that are themselves using Unity Transport. For general-purpose WebSocket communications, we recommend using a dedicated library like websocket-sharp.