static async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation) { try { while (ws.IsConnected && !cancellation.IsCancellationRequested) { String msg = await ws.ReadStringAsync(cancellation).ConfigureAwait(false); Log("Message: " + msg); ws.WriteString(msg); } } catch (Exception aex) { Log("Error Handling connection: " + aex.GetBaseException().Message); try { ws.Close(); } catch { } } finally { ws.Dispose(); } }
static async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation) { try { IWebSocketLatencyMeasure l = ws as IWebSocketLatencyMeasure; PerformanceCounters.Connected.Increment(); while (ws.IsConnected && !cancellation.IsCancellationRequested) { String msg = await ws.ReadStringAsync(cancellation).ConfigureAwait(false); PerformanceCounters.MessagesIn.Increment(); ws.WriteString(msg); PerformanceCounters.MessagesOut.Increment(); PerformanceCounters.Delay.IncrementBy(l.Latency.Ticks * Stopwatch.Frequency / 10000); PerformanceCounters.DelayBase.Increment(); } } catch (TaskCanceledException) { } catch (Exception aex) { Log("Error Handling connection: " + aex.GetBaseException().Message); try { ws.Close(); } catch { } } finally { ws.Dispose(); PerformanceCounters.Connected.Decrement(); } }
private static async Task SendPingAsync(WebSocket ws, CancellationToken cancellation) { while (true) { if (PerformanceCounters.Connected > 1000) { if (ws.IsConnected && !cancellation.IsCancellationRequested) { ws.WriteString("ping " + DateTime.Now); } } Thread.Sleep(10000); } }
private static async Task WaitForMessage(WebSocket ws, CancellationToken cancellation) { while (ws.IsConnected && !cancellation.IsCancellationRequested) { string msg = await ws.ReadStringAsync(cancellation); if (msg == null) continue; ws.WriteString(msg + " back from WebSocketTest"); } }
public void HandleMatchQueue(WebSocket ws, Dictionary<string, dynamic> msgDict) { String socketId = msgDict["uniqueId"]; // Check if ID is not already in Queue. if (!attackerQueue.Contains(socketId) && !defenderQueue.Contains(socketId)) { // Check that the ws isn't in a match already if (!wsToIdDictionary.ContainsKey(ws)) { // Queue the websocket, wait for friend. if (msgDict["msgType"].StartsWith("LogInRequest")) { Player p = new Player(ws, msgDict["username"], socketId); wsToIdDictionary.Add(ws, socketId); idToPlayerDictionary.Add(socketId, p); if (msgDict["role"].ToLower() == "attacker") { Console.WriteLine("An attacker joined the game: " + ws.RemoteEndpoint); if (defenderQueue.Count != 0) { String defenderId = defenderQueue.Dequeue(); Player defender = idToPlayerDictionary[defenderId]; Match newMatch = new Match(p, defender, this); } else { attackerQueue.Enqueue(socketId); } } else if (msgDict["role"].ToLower() == "defender") { Console.WriteLine("A defender joined the game: " + ws.RemoteEndpoint); if (attackerQueue.Count != 0) { String attackerId = attackerQueue.Dequeue(); Player attacker = idToPlayerDictionary[attackerId]; Match newMatch = new Match(attacker, p, this); } else { defenderQueue.Enqueue(socketId); } } } } } else { ws.WriteString("Please wait for a match to start."); } }