Unity Web Sockets provides a wrapper for using Web Sockets, an advanced technology that allows real-time interactive communication between the client browser and a server. It uses a completely different protocol that allows bidirectional data flow, making it unique against HTTP.
Installation · Documentation · License
Made with ♥ by Jeffrey Lanters
Install the latest stable release using the Unity Package Manager by adding the following line to your manifest.json
file located within your project's Packages directory, or by adding the Git URL to the Package Manager Window inside of Unity.
"nl.elraccoone.web-sockets": "git+https://github.com/jeffreylanters/unity-web-sockets"
The module is availble on the OpenUPM package registry, you can install the latest stable release using the OpenUPM Package manager's Command Line Tool using the following command.
openupm add nl.elraccoone.web-sockets
using UnityEngine;
using ElRaccoone.WebSockets;
public class SocketService : MonoBehaviour {
private WSConnection wsConnection = new WSConnection ("wss://localhost:3000");
private void Awake () {
this.wsConnection.OnConnected (() => {
Debug.Log ("WS Connected!");
});
this.wsConnection.OnDisconnected (() => {
Debug.Log ("WS Disconnected!");
});
this.wsConnection.OnError (error => {
Debug.Log ("WS Error " + error);
});
this.wsConnection.OnMessage (message => {
Debug.Log ("Received message " + message);
});
this.wsConnection.Connect ();
// Queue sending messages, these will always be send in this order.
this.wsConnection.SendMessage("Hello,");
this.wsConnection.SendMessage("World!");
}
private void OnDestroy () {
// A provided editor script closes all connections automatically when you
// exit play mode. Use this method to close the connection manually.
this.wsConnection.Disconnect ();
}
}