Tanoda
WebSocketServerScript.cs
Go to the documentation of this file.
1
using
System;
2
using
System.Collections;
3
using
System.Collections.Generic;
4
using
System.Text;
5
using
NaughtyAttributes;
6
using
UnityEngine
;
7
using
WebSocketSharp;
8
using
WebSocketSharp.Server;
9
10
public
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
}
Debug
UnityEngine.Debug Debug
Definition:
TanodaServer.cs:19
WebSocketServerScript.SyncObjects
Definition:
WebSocketServerScript.cs:14
WebSocketServerScript.SyncObjects.OnClose
override void OnClose(CloseEventArgs e)
Definition:
WebSocketServerScript.cs:36
WebSocketServerScript.SyncObjects.OnMessage
override void OnMessage(MessageEventArgs e)
Definition:
WebSocketServerScript.cs:16
WebSocketServerScript
Definition:
WebSocketServerScript.cs:11
WebSocketServerScript.Port
ushort Port
Definition:
WebSocketServerScript.cs:43
UnityEngine
Definition:
HSVPicker/UtilityScripts/BoxSlider.cs:7
Source
Assets
Scripts
WebSocketServerScript.cs
Generated by
1.9.3