Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neogeek/websocket-unity
Simple wrapper for the websocket-csharp library.
https://github.com/neogeek/websocket-unity
unity websocket
Last synced: 10 days ago
JSON representation
Simple wrapper for the websocket-csharp library.
- Host: GitHub
- URL: https://github.com/neogeek/websocket-unity
- Owner: neogeek
- License: mit
- Created: 2020-08-23T03:31:52.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-04T15:03:22.000Z (about 3 years ago)
- Last Synced: 2024-10-11T10:45:45.166Z (about 1 month ago)
- Topics: unity, websocket
- Language: C#
- Homepage:
- Size: 383 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket-unity
> Simple wrapper for the websocket-csharp library.
## Installation
### Unity Package Manager
#### Git
```json
{
"dependencies": {
"com.neogeek.websocket-unity": "https://github.com/neogeek/websocket-unity.git#v2.0.0",
...
}
}
```## Usage
### WebSocketController
Add the `WebSocketController` component to any GameObject.
#### Properties
- **URL** - The WebSocket URL, including the protocol (ws or wss) and the port.
- **Log Events in Editor** - For debugging purposes only. Will only work in editor and in builds marked as development.
- **Keep-Alive Timeout** - WebSockets timeout after around 60 seconds. This timeout is used to ping the server to maintain a connection.
- **Message Handler** - Attach an event to this either via code or the inspector to receive messages from the WebSocket server.### WebSocketJsonController
`WebSocketJsonController` is similar to the base `WebSocketController` class, but this class also handles custom JSON responses from a WebSocket server.
#### Example
```csharp
using Newtonsoft.Json.Linq;
using UnityEngine;
using WebSocketUnity;public struct Message
{public string type;
public string gameId;
public string gameCode;
public string playerId;
}
public class WebSocketGameLobbyClient : MonoBehaviour
{[SerializeField]
private WebSocketJsonController _webSocketJsonController;public void HandleMessage(JObject message)
{Debug.Log(message["game"]?["gameId"]);
}
public void CreateGame()
{_webSocketJsonController.Send(new Message { type = "create" });
}
public void JoinGame()
{_webSocketJsonController.Send(new Message { type = "join" });
}
}
```