2222using IBM . Watson . DeveloperCloud . Utilities ;
2323using System . Collections ;
2424using System . Collections . Generic ;
25- using System . Threading ;
26- #if UNITY_2018_2_OR_NEWER
2725using System . Security . Authentication ;
28- #endif
29-
26+ using System . Threading ;
3027#if ! NETFX_CORE
3128using UnitySDK . WebSocketSharp ;
3229#else
@@ -193,6 +190,8 @@ public Dictionary<string, string> Headers {
193190 private AutoResetEvent _receiveEvent = new AutoResetEvent ( false ) ;
194191 private Queue < Message > _receiveQueue = new Queue < Message > ( ) ;
195192 private int _receiverRoutine = 0 ;
193+ private static readonly string https = "https://" ;
194+ private static readonly string wss = "wss://" ;
196195 #endregion
197196
198197 /// <summary>
@@ -203,78 +202,76 @@ public Dictionary<string, string> Headers {
203202 public static string FixupURL ( string URL )
204203 {
205204#if UNITY_2018_2_OR_NEWER
205+ #if NET_4_6
206206 // 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." ) )
212208 {
213209 URL = URL . Replace ( "https://stream." , "wss://stream." ) ;
214210 }
215211
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.
221215 else if ( URL . StartsWith ( "https://stream-tls10." ) )
222216 {
223- URL = URL . Replace ( "https://stream-tls10." , "wss://stream." ) ;
217+ URL = URL . Replace ( "https://stream-tls10." , "wss://stream-tls10 ." ) ;
224218 }
225-
226219 // Germany
227- else if ( URL . StartsWith ( "http://gateway-fra." ) )
228- {
229- URL = URL . Replace ( "http://gateway-fra." , "ws://stream-fra." ) ;
230- }
231220 else if ( URL . StartsWith ( "https://gateway-fra." ) )
232221 {
233222 URL = URL . Replace ( "https://gateway-fra." , "wss://stream-fra." ) ;
234223 }
235-
236224 // US East
237- else if ( URL . StartsWith ( "http://gateway-wdc." ) )
238- {
239- URL = URL . Replace ( "http://gateway-wdc." , "ws://gateway-wdc." ) ;
240- }
241225 else if ( URL . StartsWith ( "https://gateway-wdc." ) )
242226 {
243227 URL = URL . Replace ( "https://gateway-wdc." , "wss://gateway-wdc." ) ;
244228 }
245-
246-
247229 // Sydney
248- else if ( URL . StartsWith ( "http://gateway-syd." ) )
249- {
250- URL = URL . Replace ( "http://gateway-syd." , "ws://gateway-syd." ) ;
251- }
252230 else if ( URL . StartsWith ( "https://gateway-syd." ) )
253231 {
254232 URL = URL . Replace ( "https://gateway-syd." , "wss://gateway-syd." ) ;
255233 }
256-
257234 else
258235 {
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)." ) ;
260255 }
256+ #endif
261257#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+ {
267262 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+ }
270264 else if ( URL . StartsWith ( "https://stream-tls10." ) )
265+ {
271266 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+ }
276274#endif
277-
278275 return URL ;
279276 }
280277
@@ -307,7 +304,7 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
307304 return connector ;
308305 }
309306
310- #region Public Functions
307+ #region Public Functions
311308 /// <summary>
312309 /// This function sends the given message object.
313310 /// </summary>
@@ -359,9 +356,9 @@ public void Close()
359356 // setting the state to closed will make the SendThread automatically exit.
360357 _connectionState = ConnectionState . CLOSED ;
361358 }
362- #endregion
359+ #endregion
363360
364- #region Private Functions
361+ #region Private Functions
365362 private IEnumerator ProcessReceiveQueue ( )
366363 {
367364 while ( _connectionState == ConnectionState . CONNECTED
@@ -392,10 +389,10 @@ private IEnumerator ProcessReceiveQueue()
392389 if ( OnClose != null )
393390 OnClose ( this ) ;
394391 }
395- #endregion
392+ #endregion
396393
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!
399396#if ! NETFX_CORE
400397 private void SendMessages ( )
401398 {
@@ -412,8 +409,12 @@ private void SendMessages()
412409 ws . OnClose += OnWSClose ;
413410 ws . OnError += OnWSError ;
414411 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
416414 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 ;
417418#endif
418419 ws . Connect ( ) ;
419420
@@ -589,6 +590,6 @@ private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocket
589590 }
590591 }
591592#endif
592- #endregion
593+ #endregion
593594 }
594595}
0 commit comments