2
2
3
3
using System ;
4
4
using System . Collections . Generic ;
5
- using System . Diagnostics ;
6
5
using System . IO ;
7
6
using System . Linq ;
8
7
using System . Text ;
12
11
using Windows . Security . Cryptography . Certificates ;
13
12
using Windows . Storage . Streams ;
14
13
using Windows . Web ;
14
+ using UnityEngine ;
15
15
16
16
namespace Unity3dAzure . WebSockets {
17
17
public class WebSocketUWP : IWebSocket {
@@ -25,19 +25,16 @@ public class WebSocketUWP : IWebSocket {
25
25
public event OnClose OnClose ;
26
26
27
27
private bool isAttached = false ;
28
+ private bool isOpened = false ;
28
29
29
30
private string url ;
30
31
private List < KeyValuePair < string , string > > headers ;
31
32
32
33
public void ConfigureWebSocket ( string url ) {
33
- ConfigureWebSocket ( url , null , null , 0 ) ;
34
+ ConfigureWebSocket ( url , null ) ;
34
35
}
35
36
36
37
public void ConfigureWebSocket ( string url , List < KeyValuePair < string , string > > headers ) {
37
- ConfigureWebSocket ( url , headers , null , 0 ) ;
38
- }
39
-
40
- public void ConfigureWebSocket ( string url , List < KeyValuePair < string , string > > headers , string origin , uint waitTime ) {
41
38
if ( socket != null ) {
42
39
throw new Exception ( "WebSocket is already configured!" ) ;
43
40
}
@@ -57,18 +54,19 @@ public void ConfigureWebSocket(string url, List<KeyValuePair<string, string>> he
57
54
58
55
public async void ConnectAsync ( ) {
59
56
if ( socket == null ) {
60
- Debug . WriteLine ( "Configure MessageWebSocket" ) ;
57
+ Debug . Log ( "Configure MessageWebSocket" ) ;
61
58
ConfigureWebSocket ( url , headers ) ;
62
59
}
63
60
AttachHandlers ( ) ;
64
61
try {
65
62
await socket . ConnectAsync ( uri ) ;
66
63
dataWriter = new DataWriter ( socket . OutputStream ) ;
64
+ isOpened = true ;
67
65
RaiseOpen ( ) ;
68
66
} catch ( Exception ex ) {
69
67
WebErrorStatus status = WebSocketError . GetStatus ( ex . GetBaseException ( ) . HResult ) ;
70
68
if ( status . Equals ( WebErrorStatus . Unknown ) ) {
71
- Debug . WriteLine ( "An unknown WebErrorStatus exception occurred." ) ;
69
+ Debug . LogError ( "An unknown WebErrorStatus exception occurred." ) ;
72
70
} else {
73
71
RaiseError ( "Error: MessageWebSocket failed to connect: " + status . ToString ( ) ) ;
74
72
}
@@ -81,14 +79,15 @@ public void CloseAsync() {
81
79
return ;
82
80
}
83
81
try {
82
+ isOpened = false ;
84
83
socket . Close ( 1000 , "User closed" ) ;
85
84
} catch ( Exception ex ) {
86
85
RaiseError ( ex . Message ) ;
87
86
}
88
87
}
89
88
90
89
public bool IsOpen ( ) {
91
- if ( socket != null && isAttached ) {
90
+ if ( socket != null && isOpened ) {
92
91
return true ;
93
92
}
94
93
return false ;
@@ -106,15 +105,6 @@ public async void SendAsync(byte[] data, Action<bool> completed = null) {
106
105
await SendAsyncData ( data , completed ) ;
107
106
}
108
107
109
- public void SendAsync ( FileInfo fileInfo , Action < bool > completed = null ) {
110
- using ( FileStream fs = fileInfo . OpenRead ( ) ) {
111
- byte [ ] buffer = new byte [ 1024 ] ;
112
- while ( fs . Read ( buffer , 0 , buffer . Length ) > 0 ) {
113
- SendAsync ( buffer , completed ) ;
114
- }
115
- }
116
- }
117
-
118
108
private async Task SendAsyncText ( string message , Action < bool > completed = null ) {
119
109
try {
120
110
await dataWriter . FlushAsync ( ) ;
@@ -153,7 +143,7 @@ private async Task SendAsyncData(byte[] Data, Action<bool> completed = null) {
153
143
}
154
144
}
155
145
156
- #region WebSocket Handlers
146
+ #region WebSocket Handlers
157
147
158
148
private void AttachHandlers ( ) {
159
149
if ( isAttached ) {
@@ -169,11 +159,9 @@ private void UnattachHandlers() {
169
159
return ;
170
160
}
171
161
isAttached = false ;
172
- try {
162
+ if ( isOpened ) {
173
163
socket . MessageReceived -= HandleOnMessage ;
174
164
socket . Closed -= HandleOnClose ;
175
- } catch ( Exception ex ) {
176
- Debug . WriteLine ( "Socket closed: " + ex . Message ) ;
177
165
}
178
166
}
179
167
@@ -182,6 +170,8 @@ private void Dispose() {
182
170
dataWriter = null ;
183
171
socket . Dispose ( ) ;
184
172
socket = null ;
173
+ isAttached = false ;
174
+ isOpened = false ;
185
175
}
186
176
187
177
private void RaiseError ( string message ) {
@@ -226,7 +216,7 @@ private void HandleOnClose(object sender, WebSocketClosedEventArgs args) {
226
216
Dispose ( ) ;
227
217
}
228
218
229
- #endregion
219
+ #endregion
230
220
231
221
private Uri WebSocketUri ( string url ) {
232
222
Uri webSocketUri ;
0 commit comments