|
2 | 2 | A simple, zero-dependency WebSocket server for Unity. |
3 | 3 |
|
4 | 4 | ## Usage |
| 5 | +First import the package and set up the scene: |
5 | 6 | * Download the UnityPackage from the [Releases page](https://github.com/shaunabanana/unity-websocket-server/releases). Import. |
6 | 7 | * Create an empty GameObject, assign the script `WebSocketServer.cs`. |
7 | 8 | * Input the IP and port of the server. |
8 | | -* Add callbacks to the `onMessage` UnityEvent. |
| 9 | + |
| 10 | +### In Inspector GUI |
| 11 | +The easiest way is to use the inspector. Add callbacks to the `onMessage`, `onMessage`, or `onClose` UnityEvent. |
| 12 | + |
| 13 | +### Using class inheritance |
| 14 | +Alternatively, you can inherit from `WebSocketServer`. The following code shows you how: |
| 15 | +```csharp |
| 16 | +using WebSocketServer; |
| 17 | + |
| 18 | +public class MyWebSocketServer: WebSocketServer { |
| 19 | + |
| 20 | + public void OnOpen(WebSocketConnection connection) { |
| 21 | + // Here, (string)connection.id gives you a unique ID to identify the client. |
| 22 | + Debug.Log(connection.id); |
| 23 | + } |
| 24 | + |
| 25 | + public void OnMessage(WebSocketMessage message) { |
| 26 | + // Here, message.connection gives you the connection that send the message. |
| 27 | + // (string)message.data gives you the message content. |
| 28 | + Debug.Log(message.data); |
| 29 | + } |
| 30 | + |
| 31 | + public void OnOpen(WebSocketConnection connection) { |
| 32 | + // Here is the same as OnOpen |
| 33 | + Debug.Log(connection.id); |
| 34 | + } |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +``` |
9 | 39 |
|
10 | 40 | ## Tested on |
11 | 41 | > If you find this script works/doesn't work for you, please let me know by creating an [issue](https://github.com/shaunabanana/unity-websocket-server/issues) or emailing me at [email protected]. |
|
0 commit comments