Esempio n. 1
0
        public async Task HandleWebSocketIncomingStats(HttpContext context)
        {
            WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

            Console.WriteLine(String.Format("wss connected Stats sessionID = {0} socket = {1}", context.Session.Id, webSocket.GetHashCode()));
            StatsSessionSockets.AddOrUpdate(Guid.Parse(context.Session.Id), webSocket, (key, _) => webSocket);

            var msg = Encoding.UTF8.GetBytes(String.Format("Stats socket {0}", context.Session.Id));

            var buffer = new ArraySegment <byte>(msg, 0, msg.Length);
            await webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);

            Console.WriteLine("sent async connection");
            var buf2 = new byte[1024 * 4];
            WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buf2), CancellationToken.None);

            while (!result.CloseStatus.HasValue)
            {
                result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buf2), CancellationToken.None);
            }

            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);

            HandleStatsWebSocketClosing(context);
        }
Esempio n. 2
0
        public async Task UpdateAdminStats(Dictionary <DateTime, int[]> statistics)
        {
            DateTime today       = DateTime.Today;
            string   statsString = JsonConvert.SerializeObject(statistics);

            //string statString = string.Format('"date": {0}, "values": [{1}]"', today.ToShortDateString(), string.Join(", ", statistics[today]));
            Console.WriteLine(statsString);
            byte[] msg = Encoding.UTF8.GetBytes("stats:" + statsString);// TODO: test data
            // "stats: { date: dd/mm/yyyy, stats: [0, 1, 2, 3, 4] }"
            ArraySegment <byte> buffer = new ArraySegment <byte>(msg, 0, msg.Length);

            foreach (KeyValuePair <Guid, Guid> adminSession in AdminSessions)
            {
                if (StatsSessionSockets.TryGetValue(adminSession.Value, out WebSocket socket))
                {
                    Console.WriteLine(string.Format("update stats for session {0} admin {1}", adminSession.Value, adminSession.Key));

                    await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
                }
            }
        }
Esempio n. 3
0
 public void HandleStatsWebSocketClosing(HttpContext context)
 {
     StatsSessionSockets.TryRemove(Guid.Parse(context.Session.Id), out _);
     Console.WriteLine(String.Format("closed stats wss for session {0}", Guid.Parse(context.Session.Id)));
 }