Tanoda
WebSocketServerScript.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5using NaughtyAttributes;
6using UnityEngine;
7using WebSocketSharp;
8using WebSocketSharp.Server;
9
10public class WebSocketServerScript : MonoBehaviour
11{
12
13 public class SyncObjects : WebSocketBehavior
14 {
15 private int currentPlayerCount = 0;
16 protected override void OnMessage (MessageEventArgs e)
17 {
18 if (e.Data == "requestPlayerNum")
19 {
20 Send("responsePlayerNum=" + currentPlayerCount++);
21 return;
22 }
23
24 var msg = e.Data == "dummy thicc"
25 ? "nice!"
26 : "Msg recieved!";
27 #if DEBUG
28 if (e.IsText)
29 Debug.Log("WS SERVER RECEIVED: " + Encoding.UTF8.GetString(e.RawData));
30 else
31 Debug.Log("WS SERVER RECEIVED byte array");
32 #endif
33 Send (msg);
34 }
35
36 protected override void OnClose(CloseEventArgs e)
37 {
38 base.OnClose(e);
39 currentPlayerCount--;
40 }
41 }
42
43 public ushort Port = 5656;
44
45
46 // Start is called before the first frame update
47 void Start()
48 {
49 WebSocketServer wss = new WebSocketServer(Port);
50 wss.AddWebSocketService<SyncObjects>("/sync");
51 wss.Start();
52 Debug.Log("wss started: " + wss.IsListening);
53 }
54}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
override void OnClose(CloseEventArgs e)
override void OnMessage(MessageEventArgs e)