UnityWebSocket

repository·master·Indexed 22 days ago

https://github.com/psygames/unitywebsocket

A WebSocket client library for Unity providing asynchronous communication capabilities. It supports Unity 2018.3 or higher and features a WebSocket class with methods for connecting, sending string or byte array data, and closing connections, along with event handlers for OnOpen, OnClose, OnMessage, and OnError.

Tokens
1.1K
Snippets
4
Records
9
Agent score
29%

What's inside UnityWebSocket

  1. Install UnityWebSocket via Unity Package Manager

    master

    The recommended way to install the library is using the Unity Package Manager (UPM).

    1. Open Window > Package Manager in the Unity menu bar.
    2. Click the + icon at the top left.
    3. Select Add package from git URL....
    4. Enter https://github.com/psygames/UnityWebSocket.git#upm and confirm.
    https://github.com/psygames/UnityWebSocket.git#upm
  2. Quick Start with UnityWebSocket

    master

    To use UnityWebSocket, import the UnityWebSocket namespace, instantiate a WebSocket with a server address, register event callbacks for connection lifecycle and messages, and call ConnectAsync() to establish the connection.

    Requirements:

    • Unity 2018.3 or higher.
    // the namespace
    using UnityWebSocket;
    
    // create instance
    string address = "ws://echo.websocket.org";
    WebSocket socket = new WebSocket(address);
    
    // register callback
    socket.OnOpen += OnOpen;
    socket.OnClose += OnClose;
    socket.OnMessage += OnMessage;
    socket.OnError += OnError;
    
    // connect
    socket.ConnectAsync();
    
    // send string data 
    socket.SendAsync(str);
    // or send byte[] data (suggested)
    socket.SendAsync(bytes); 
    
    // close connection
    socket.CloseAsync();
  3. Install UnityWebSocket via Package Manager

    master

    The recommended way to install UnityWebSocket is using Unity's Package Manager via a Git URL.

    1. Open Window/Package Manager in the Unity menu bar.
    2. Click the + icon in the top-left corner.
    3. Select Add package from git URL....
    4. Enter the following URL: https://github.com/psygames/UnityWebSocket.git#upm
    5. Confirm to begin the installation.
    https://github.com/psygames/UnityWebSocket.git#upm
  4. Basic usage of the WebSocket class

    master

    To use UnityWebSocket, import the UnityWebSocket namespace and instantiate a WebSocket object with a server address. You can register event handlers for connection lifecycle events and use asynchronous methods to connect, send data, and close the connection.

    Key Methods:

    • ConnectAsync(): Initiates the connection.
    • SendAsync(string): Sends string data.
    • SendAsync(byte[]): Sends byte array data (recommended for performance).
    • CloseAsync(): Closes the connection.

    Events:

    • OnOpen
    • OnClose
    • OnMessage
    • OnError
    // 命名空间
    using UnityWebSocket;
    
    // 创建实例
    string address = "ws://echo.websocket.org";
    WebSocket socket = new WebSocket(address);
    
    // 注册回调
    socket.OnOpen += OnOpen;
    socket.OnClose += OnClose;
    socket.OnMessage += OnMessage;
    socket.OnError += OnError;
    
    // 连接
    socket.ConnectAsync();
    
    // 发送 string 类型数据
    socket.SendAsync(str); 
    
    // 或者 发送 byte[] 类型数据(建议使用)
    socket.SendAsync(bytes); 
    
    // 关闭连接
    socket.CloseAsync();