https://github.com/endel/NativeWebSocket
🔌 WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, iOS, UWP)
https://github.com/endel/NativeWebSocket
unity-asset unity3d-plugin webgl websockets
Last synced: about 1 month ago
JSON representation
🔌 WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, iOS, UWP)
- Host: GitHub
- URL: https://github.com/endel/NativeWebSocket
- Owner: endel
- License: other
- Created: 2019-08-06T03:27:58.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-24T19:38:36.000Z (over 1 year ago)
- Last Synced: 2024-10-29T17:11:57.977Z (7 months ago)
- Topics: unity-asset, unity3d-plugin, webgl, websockets
- Language: C#
- Homepage:
- Size: 98.6 KB
- Stars: 1,240
- Watchers: 32
- Forks: 158
- Open Issues: 53
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
This is the simplest and easiest WebSocket library for Unity you can find!
- No external DLL's required (uses built-in `System.Net.WebSockets`)
- WebGL/HTML5 support
- Supports all major build targets
- Very simple API
- (Used in [Colyseus Unity SDK](https://github.com/colyseus/colyseus-unity-sdk))### Consider supporting my work on [Patreon](https://patreon.com/endel) | [Ko-fi](https://ko-fi.com/endeld) | [PayPal](https://www.paypal.me/endeld)
[](https://patreon.com/endel)
## Installation
*Requires Unity 2019.1+ with .NET 4.x+ Runtime*
### Install via UPM (Unity Package Manager)
1. Open Unity
2. Open Package Manager Window
3. Click Add Package From Git URL
4. Enter URL: ```https://github.com/endel/NativeWebSocket.git#upm```### Install manually
1. [Download this project](https://github.com/endel/NativeWebSocket/archive/master.zip)
2. Copy the sources from `NativeWebSocket/Assets/WebSocket` into your `Assets` directory.## Usage
```csharp
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;using NativeWebSocket;
public class Connection : MonoBehaviour
{
WebSocket websocket;// Start is called before the first frame update
async void Start()
{
websocket = new WebSocket("ws://localhost:3000");websocket.OnOpen += () =>
{
Debug.Log("Connection open!");
};websocket.OnError += (e) =>
{
Debug.Log("Error! " + e);
};websocket.OnClose += (e) =>
{
Debug.Log("Connection closed!");
};websocket.OnMessage += (bytes) =>
{
Debug.Log("OnMessage!");
Debug.Log(bytes);// getting the message as a string
// var message = System.Text.Encoding.UTF8.GetString(bytes);
// Debug.Log("OnMessage! " + message);
};// Keep sending messages at every 0.3s
InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);// waiting for messages
await websocket.Connect();
}void Update()
{
#if !UNITY_WEBGL || UNITY_EDITOR
websocket.DispatchMessageQueue();
#endif
}async void SendWebSocketMessage()
{
if (websocket.State == WebSocketState.Open)
{
// Sending bytes
await websocket.Send(new byte[] { 10, 20, 30 });// Sending plain text
await websocket.SendText("plain text message");
}
}private async void OnApplicationQuit()
{
await websocket.Close();
}}
```# Demonstration
**1.** Start the local WebSocket server:
```
cd Server
npm install
npm start
```**2.** Open the `NativeWebSocket/Assets/WebSocketExample/WebSocketExampleScene.unity` on Unity and Run.
## Acknowledgements
Big thanks to [Jiri Hybek](https://github.com/jirihybek/unity-websocket-webgl).
This implementation is based on his work.## License
Apache 2.0