-
Notifications
You must be signed in to change notification settings - Fork 6
/
UnityWebSocketScript.cs
87 lines (63 loc) · 1.82 KB
/
UnityWebSocketScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Unity3dAzure.WebSockets {
public sealed class UnityWebSocketScript : UnityWebSocket {
// Unity Inspector fields
[SerializeField]
private string webSocketUri = "ws://127.0.0.1:8080";
[SerializeField]
private bool AutoConnect = false;
[Header ("Optional config")]
[SerializeField]
private List<UnityKeyValue> headers;
#region Web Socket connection
void Start () {
// Config Websocket
WebSocketUri = webSocketUri;
Headers = headers;
if (AutoConnect) {
Connect ();
}
}
void OnDisable () {
Close ();
}
#endregion
#region Web Socket methods
/*
public override void Connect () {
ConnectWebSocket ();
}
public override void Close () {
DisconnectWebSocket ();
}
*/
#endregion
#region Web Socket handlers
/*
protected override void OnWebSocketOpen (object sender, EventArgs e) {
Debug.Log ("Web socket is open");
}
protected override void OnWebSocketClose (object sender, WebSocketCloseEventArgs e) {
Debug.Log ("Web socket closed with reason: " + e.Reason);
if (!e.WasClean) {
DisconnectWebSocket ();
}
DettachHandlers();
}
protected override void OnWebSocketMessage (object sender, WebSocketMessageEventArgs e) {
Debug.LogFormat ("Web socket {1} message:\n{0}", e.Data, e.IsBinary ? "binary" : "string");
// Raise web socket data handler event
if (OnData != null) {
OnData (e.RawData, e.Data, e.IsBinary);
}
}
protected override void OnWebSocketError (object sender, WebSocketErrorEventArgs e) {
Debug.LogError ("Web socket error: " + e.Message);
DisconnectWebSocket ();
}
*/
#endregion
}
}