-
Notifications
You must be signed in to change notification settings - Fork 6
/
UnityWebSocket.cs
150 lines (123 loc) · 4.09 KB
/
UnityWebSocket.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
namespace Unity3dAzure.WebSockets {
public abstract class UnityWebSocket : MonoBehaviour {
// Web Socket data delegate
public delegate void Data (byte[] rawData, string data, Boolean isBinary);
public static Data OnData;
protected string WebSocketUri;
protected List<UnityKeyValue> Headers;
protected IWebSocket _ws;
protected bool isAttached = false;
#region Web Socket methods
public virtual void Connect () {
ConnectWebSocket ();
}
public virtual void Close () {
DisconnectWebSocket ();
}
#endregion
#region Web Socket handlers
protected virtual void OnWebSocketOpen (object sender, EventArgs e) {
Debug.Log ("Web socket is open");
}
protected virtual void OnWebSocketClose (object sender, WebSocketCloseEventArgs e) {
Debug.Log ("Web socket closed with reason: " + e.Reason);
DettachHandlers();
}
protected virtual 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 virtual void OnWebSocketError (object sender, WebSocketErrorEventArgs e) {
Debug.LogError ("Web socket error: " + e.Message);
DisconnectWebSocket ();
}
#endregion
public void SendText (string text, Action<bool> callback = null) {
if (_ws == null || !_ws.IsOpen()) {
Debug.LogWarning("Web socket is not available to send text message. Try connecting?");
return;
}
_ws.SendAsync (text, callback);
}
public void SendUTF8Text (string text, Action<bool> callback = null) {
byte[] data = Encoding.UTF8.GetBytes (text);
SendBytes (data, callback);
}
public virtual void SendInputText (InputField inputField) {
SendText (inputField.text);
}
public void SendBytes (byte[] data, Action<bool> callback = null) {
if (_ws == null || !_ws.IsOpen()) {
Debug.LogWarning("Web socket is not available to send bytes. Try connecting?");
return;
}
_ws.SendAsync (data, callback);
}
protected void ConnectWebSocket () {
if (string.IsNullOrEmpty (WebSocketUri)) {
Debug.LogError ("WebSocketUri must be set");
return;
}
if (_ws == null || !_ws.IsConfigured()) {
var customHeaders = new List<KeyValuePair<string, string>>();
if (Headers != null) {
foreach (UnityKeyValue header in Headers) {
customHeaders.Add(new KeyValuePair<string, string>(header.key, header.value));
}
}
Debug.Log ("Create Web Socket: " + WebSocketUri);
#if ENABLE_WINMD_SUPPORT
Debug.Log ("Using UWP Web Socket");
_ws = new WebSocketUWP();
#elif UNITY_EDITOR || ENABLE_MONO
Debug.Log("Using Mono Web Socket");
_ws = new WebSocketMono();
#endif
_ws.ConfigureWebSocket(WebSocketUri, customHeaders);
}
if (!isAttached) {
Debug.Log ("Connect Web Socket: " + _ws.Url());
AttachHandlers();
_ws.ConnectAsync ();
}
}
protected void DisconnectWebSocket () {
if (_ws != null && isAttached) {
Debug.Log ("Disconnect Web Socket");
_ws.CloseAsync ();
}
}
protected void AttachHandlers() {
if (isAttached) {
return;
}
isAttached = true;
_ws.OnError += OnWebSocketError;
_ws.OnOpen += OnWebSocketOpen;
_ws.OnMessage += OnWebSocketMessage;
_ws.OnClose += OnWebSocketClose;
}
protected void DettachHandlers() {
if (!isAttached) {
return;
}
isAttached = false;
_ws.OnError -= OnWebSocketError;
_ws.OnOpen -= OnWebSocketOpen;
_ws.OnMessage -= OnWebSocketMessage;
_ws.OnClose -= OnWebSocketClose;
}
}
}