Skip to content

Commit d943a87

Browse files
committed
2 parents 702e686 + e1ad8b5 commit d943a87

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,40 @@
22
A simple, zero-dependency WebSocket server for Unity.
33

44
## Usage
5+
First import the package and set up the scene:
56
* Download the UnityPackage from the [Releases page](https://github.com/shaunabanana/unity-websocket-server/releases). Import.
67
* Create an empty GameObject, assign the script `WebSocketServer.cs`.
78
* 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+
```
939

1040
## Tested on
1141
> 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

Comments
 (0)