22
22
using IBM . Watson . DeveloperCloud . Utilities ;
23
23
using System . Collections ;
24
24
using System . Collections . Generic ;
25
- using System . Threading ;
26
- #if UNITY_2018_2_OR_NEWER
27
25
using System . Security . Authentication ;
28
- #endif
29
-
26
+ using System . Threading ;
30
27
#if ! NETFX_CORE
31
28
using UnitySDK . WebSocketSharp ;
32
29
#else
@@ -193,6 +190,8 @@ public Dictionary<string, string> Headers {
193
190
private AutoResetEvent _receiveEvent = new AutoResetEvent ( false ) ;
194
191
private Queue < Message > _receiveQueue = new Queue < Message > ( ) ;
195
192
private int _receiverRoutine = 0 ;
193
+ private static readonly string https = "https://" ;
194
+ private static readonly string wss = "wss://" ;
196
195
#endregion
197
196
198
197
/// <summary>
@@ -203,78 +202,76 @@ public Dictionary<string, string> Headers {
203
202
public static string FixupURL ( string URL )
204
203
{
205
204
#if UNITY_2018_2_OR_NEWER
205
+ #if NET_4_6
206
206
// Use standard endpoints since 2018.2 supports TLS 1.2
207
- if ( URL . StartsWith ( "http://stream." ) )
208
- {
209
- URL = URL . Replace ( "http://stream." , "ws://stream." ) ;
210
- }
211
- else if ( URL . StartsWith ( "https://stream." ) )
207
+ if ( URL . StartsWith ( "https://stream." ) )
212
208
{
213
209
URL = URL . Replace ( "https://stream." , "wss://stream." ) ;
214
210
}
215
211
216
- // TLS 1.0 endpoint
217
- else if ( URL . StartsWith ( "http://stream-tls10." ) )
218
- {
219
- URL = URL . Replace ( "http://stream-tls10." , "ws://stream." ) ;
220
- }
212
+ // TLS 1.0 endpoint - Do not change this to TLS 1.2 endpoint since
213
+ // users may need to use the TLS 1.0 endpoint because of different
214
+ // platforms.
221
215
else if ( URL . StartsWith ( "https://stream-tls10." ) )
222
216
{
223
- URL = URL . Replace ( "https://stream-tls10." , "wss://stream." ) ;
217
+ URL = URL . Replace ( "https://stream-tls10." , "wss://stream-tls10 ." ) ;
224
218
}
225
-
226
219
// Germany
227
- else if ( URL . StartsWith ( "http://gateway-fra." ) )
228
- {
229
- URL = URL . Replace ( "http://gateway-fra." , "ws://stream-fra." ) ;
230
- }
231
220
else if ( URL . StartsWith ( "https://gateway-fra." ) )
232
221
{
233
222
URL = URL . Replace ( "https://gateway-fra." , "wss://stream-fra." ) ;
234
223
}
235
-
236
224
// US East
237
- else if ( URL . StartsWith ( "http://gateway-wdc." ) )
238
- {
239
- URL = URL . Replace ( "http://gateway-wdc." , "ws://gateway-wdc." ) ;
240
- }
241
225
else if ( URL . StartsWith ( "https://gateway-wdc." ) )
242
226
{
243
227
URL = URL . Replace ( "https://gateway-wdc." , "wss://gateway-wdc." ) ;
244
228
}
245
-
246
-
247
229
// Sydney
248
- else if ( URL . StartsWith ( "http://gateway-syd." ) )
249
- {
250
- URL = URL . Replace ( "http://gateway-syd." , "ws://gateway-syd." ) ;
251
- }
252
230
else if ( URL . StartsWith ( "https://gateway-syd." ) )
253
231
{
254
232
URL = URL . Replace ( "https://gateway-syd." , "wss://gateway-syd." ) ;
255
233
}
256
-
257
234
else
258
235
{
259
- Log . Warning ( "WSConnector" , "No case for URL for wss://. Leaving URL unchanged." ) ;
236
+ URL = URL . Replace ( https , wss ) ;
237
+ Log . Warning ( "WSConnector" , "No case for URL for wss://. Replacing https:// with wss://." ) ;
238
+ }
239
+ #else
240
+ // Use TLS 1.0 endpoint if user is on .NET 3.5. US South is the
241
+ // only region that supports this endpoint.
242
+ if ( URL . StartsWith ( "https://stream." ) )
243
+ {
244
+ URL = URL . Replace ( "https://stream." , "wss://stream-tls10." ) ;
245
+ }
246
+ else if ( URL . StartsWith ( "https://stream-tls10." ) )
247
+ {
248
+ URL = URL . Replace ( "https://stream-tls10." , "wss://stream-tls10." ) ;
249
+ }
250
+ else
251
+ {
252
+ URL = URL . Replace ( https , wss ) ;
253
+ Log . Warning ( "WSConnector" , "No case for URL for wss://. Replacing https:// with wss://." ) ;
254
+ Log . Warning ( "WSConnector" , "Streaming with TLS 1.0 is only available in US South. Please create your Speech to Text instance in US South. Alternatviely, use Unity 2018.2 with .NET 4.x Scripting Runtime Version enabled (File > Build Settings > Player Settings > Other Settings > Scripting Runtime Version)." ) ;
260
255
}
256
+ #endif
261
257
#else
262
- // Redirect to TLS 1.0 endpoints.
263
- // Note frankfurt endpoint does not support TLS 1.0.
264
- if ( URL . StartsWith ( "http://stream." ) )
265
- URL = URL . Replace ( "http://stream." , "ws://stream-tls10." ) ;
266
- else if ( URL . StartsWith ( "https://stream." ) )
258
+ // Use TLS 1.0 endpoint if user is on .NET 3.5 or 4.6 if using Unity 2018.1 or older.
259
+ // US South is the only region that supports this endpoint.
260
+ if ( URL . StartsWith ( "https://stream." ) )
261
+ {
267
262
URL = URL . Replace ( "https://stream." , "wss://stream-tls10." ) ;
268
- else if ( URL . StartsWith ( "http://stream-tls10." ) )
269
- URL = URL . Replace ( "http://stream-tls10." , "ws://stream-tls10." ) ;
263
+ }
270
264
else if ( URL . StartsWith ( "https://stream-tls10." ) )
265
+ {
271
266
URL = URL . Replace ( "https://stream-tls10." , "wss://stream-tls10." ) ;
272
- else if ( URL . StartsWith ( "http://stream-fra." ) )
273
- URL = URL . Replace ( "http://stream-fra." , "ws://stream-fra." ) ;
274
- else if ( URL . StartsWith ( "https://stream-fra." ) )
275
- URL = URL . Replace ( "https://stream-fra." , "wss://stream-fra." ) ;
267
+ }
268
+ else
269
+ {
270
+ URL = URL . Replace ( https , wss ) ;
271
+ Log . Warning ( "WSConnector" , "No case for URL for wss://. Replacing https:// with wss://." ) ;
272
+ Log . Warning ( "WSConnector" , "Streaming with TLS 1.0 is only available in US South. Please create your Speech to Text instance in US South. Alternatviely, use Unity 2018.2 with .NET 4.x Scripting Runtime Version enabled (File > Build Settings > Player Settings > Other Settings > Scripting Runtime Version)." ) ;
273
+ }
276
274
#endif
277
-
278
275
return URL ;
279
276
}
280
277
@@ -307,7 +304,7 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
307
304
return connector ;
308
305
}
309
306
310
- #region Public Functions
307
+ #region Public Functions
311
308
/// <summary>
312
309
/// This function sends the given message object.
313
310
/// </summary>
@@ -359,9 +356,9 @@ public void Close()
359
356
// setting the state to closed will make the SendThread automatically exit.
360
357
_connectionState = ConnectionState . CLOSED ;
361
358
}
362
- #endregion
359
+ #endregion
363
360
364
- #region Private Functions
361
+ #region Private Functions
365
362
private IEnumerator ProcessReceiveQueue ( )
366
363
{
367
364
while ( _connectionState == ConnectionState . CONNECTED
@@ -392,10 +389,10 @@ private IEnumerator ProcessReceiveQueue()
392
389
if ( OnClose != null )
393
390
OnClose ( this ) ;
394
391
}
395
- #endregion
392
+ #endregion
396
393
397
- #region Threaded Functions
398
- // NOTE: ALl functions in this region are operating in a background thread, do NOT call any Unity functions!
394
+ #region Threaded Functions
395
+ // NOTE: All functions in this region are operating in a background thread, do NOT call any Unity functions!
399
396
#if ! NETFX_CORE
400
397
private void SendMessages ( )
401
398
{
@@ -412,8 +409,12 @@ private void SendMessages()
412
409
ws . OnClose += OnWSClose ;
413
410
ws . OnError += OnWSError ;
414
411
ws . OnMessage += OnWSMessage ;
415
- #if UNITY_2018_2_OR_NEWER
412
+ #if NET_4_6
413
+ // Enable TLS 1.1 and TLS 1.2 if we are on .NET 4.x
416
414
ws . SslConfiguration . EnabledSslProtocols = SslProtocols . Tls12 | SslProtocols . Tls11 | SslProtocols . Tls ;
415
+ #else
416
+ // .NET 3.x does not support TLS 1.1 or TLS 1.2
417
+ ws . SslConfiguration . EnabledSslProtocols = SslProtocols . Tls ;
417
418
#endif
418
419
ws . Connect ( ) ;
419
420
@@ -589,6 +590,6 @@ private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocket
589
590
}
590
591
}
591
592
#endif
592
- #endregion
593
+ #endregion
593
594
}
594
595
}
0 commit comments