Example #1
0
        private static async Task EchoAllIncomingMessagesAsync(WebSocket webSocket, CancellationToken cancellation)
        {
            Console.WriteLine("Client '" + webSocket.RemoteEndpoint + "' connected.");
            var sw = new Stopwatch();

            try
            {
                while (webSocket.IsConnected && !cancellation.IsCancellationRequested)
                {
                    try
                    {
                        var messageText = await webSocket.ReadStringAsync(cancellation).ConfigureAwait(false);

                        if (messageText == null)
                        {
                            break; // webSocket is disconnected
                        }
                        Console.WriteLine("Client '" + webSocket.RemoteEndpoint + "' recived: " + messageText + ".");

                        sw.Restart();

                        messageText = WebSocketHandler(messageText);

                        await webSocket.WriteStringAsync(messageText, cancellation).ConfigureAwait(false);

                        Console.WriteLine("Client '" + webSocket.RemoteEndpoint + "' sent: " + messageText + ".");

                        sw.Stop();
                    }
                    catch (TaskCanceledException)
                    {
                        break;
                    }
                    catch (Exception readWriteError)
                    {
                        Console.WriteLine("An error occurred while reading/writing echo message.", readWriteError);
                        await webSocket.CloseAsync().ConfigureAwait(false);
                    }
                }
            }
            finally
            {
                webSocket.Dispose();
                Console.WriteLine("Client '" + webSocket.RemoteEndpoint + "' disconnected.");
            }
        }
Example #2
0
        private static async Task WaitForAuthenticateMessage(WebSocket ws, CancellationToken cancellation)
        {
            while (ws.IsConnected && !cancellation.IsCancellationRequested)
            {
                string json = await ws.ReadStringAsync(cancellation).ConfigureAwait(false);
                if (json == null)
                    continue;

                var auth = JsonConvert.DeserializeObject<AuthenticateMessage>(json);
                if (!string.IsNullOrEmpty(auth.Id))
                {
                    Interlocked.Increment(ref PerformanceCounters.Authenticated);
                    ws.WriteStringAsync(auth.Id + "_OK", cancellation).Wait(cancellation);
                    Console.WriteLine("Authenticated " + PerformanceCounters.Authenticated);

                    break;
                }
            }
        }
 private void Push(WebSocket client, string data)
 {
     client.WriteStringAsync(data, CancellationToken.None);
 }
 private static void SendWelcomeMessage(WebSocket clientSocket)
 {
     var welcomeMessage = new JavaScriptSerializer().Serialize(new
     {
         endpoint = "connectedToUlterius",
         results = new
         {
             message = "Ulterius server online!",
             authRequired = true
         }
     });
     clientSocket.WriteStringAsync(welcomeMessage, CancellationToken.None);
 }