void initializeWebSocket() { if (wsURL != null) { #if WINDOWS_UWP ws = new MessageWebSocket(); ws.Control.MessageType = SocketMessageType.Utf8; #else ws = new WebSocket(wsURL); #endif } else { Debug.Log("Web Socket intialize failure."); return; } #if WINDOWS_UWP ws.MessageReceived += Websocket_MessageReceived; var task = Task.Run(async() => { await ws.ConnectAsync(new Uri(wsURL)); }); task.Wait(); #else ws.OnOpen += onOpen; ws.OnMessage += onMessage; ws.OnError += onError; ws.OnClose += onClose; ws.Connect(); #endif }
public async Task <bool> ConnectAsync() { TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); EventHandler onAuthenticated = null; onAuthenticated = (s, e) => { Authenticated -= onAuthenticated; if (IsAuthenticated) { tcs.SetResult(IsAuthenticated); } else { tcs.TrySetException(new Exception("Authentication failed")); } }; Authenticated += onAuthenticated; ws = new MessageWebSocket(); ws.MessageReceived += OnWsMessageReceived; ws.Connect(Endpoint.ToString()); return(await tcs.Task); }
public void Connect() { #if WINDOWS_UWP var task = Task.Run(async() => { await ws.ConnectAsync(new Uri(address)); }); task.Wait(); #else ws.Connect(); #endif }