Skip to content

Latest commit

 

History

History
95 lines (72 loc) · 2.32 KB

File metadata and controls

95 lines (72 loc) · 2.32 KB

Unity WebSocket Client (Multiplayer Game)

Alt Text

This is an example of a Unity client that uses WebSockets to send the current players information.

This WebSocket client is working with a NodeJS server on multiplayer-websocket-server-node.

Installation

_Requires Unity 2020.3.24f1+

Install manually

  1. Download the unity package
  2. Import the package in an empty Unity project.

Usage

The script Assets/Scripts/Networking/WebSocketClient.cs handles the WebSocket connection

using UnityEngine;
using WebSocketSharp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class WebSocketClient : MonoBehaviour
{
  public const string WSURL_KEY = "wsurl";
  private static WebSocketClient instance;

  WebSocket ws;

  #region MonoBehaviour
  void Awake()
  {
    instance = this;
  }

  private void Start()
  {
    ws = new WebSocket(PlayerPrefs.GetString(WSURL_KEY)); //Get the Websocket URL from player prefs
    ws.OnOpen += OnWSOpen;
    ws.OnMessage += OnWSMessageReceived;
    ws.Connect();
  }

  void OnApplicationQuit()
  {
    ws.Close();
  }

  void OnDestroy()
  {
    instance = null;
  }
  #endregion

  #region Static Functions
  public static void SendMsg(string message)
  {
    instance.ws.Send(message);
  }

  public static void CloseConnection()
  {
    instance.ws.Close();
  }
  #endregion

  #region WebSocket Callbacks
  void OnWSOpen(object sender, System.EventArgs e)
  {
    //Send connection data to spawn
    ws.Send('{}') //JSON with connection data
  }

  void OnWSMessageReceived(object sender, MessageEventArgs e)
  {
    var received = JObject.Parse(e.Data);
    switch ((string)received[WSJsonKeys.ACTION])
    {
      //Case for each action (Can be LocalConnection, Connected, Movement, UserLeave)
    }
  }
  #endregion
}

Demonstration

1. Start the local WebSocket server

2. Run the main scene in Unity or any of the builds for Windows or Mac OS.