async Task MessageLoop(Session session) { try { // now loop for any incoming (we just echo) while (session.Socket.State == WebSocketState.Open) { await session.ReceiveAsync(); if (session.Result.CloseStatus.HasValue) break; Message m = Message.Deserialize(new ArraySegment<byte>(session.Buffer, 0, session.Result.Count)); switch (m.Type) { case MessageType.Subscribe: this.Factory.AddSubscription(session, (Subscribe) m); break; case MessageType.GetSubscriptions: var list = this.Factory.GetSubscriptions(session); var response = new Subscriptions(); response.List = list; await session.SendAsync(response.Serialize()); break; default: await DoUnhandled(session, m); break; } } } catch { } }
async Task MessageLoop(Session session) { try { // now loop for any incoming (we just echo) while (session.Socket.State == WebSocketState.Open) { await session.ReceiveAsync(); if (session.Result.CloseStatus.HasValue) { break; } Message m = Message.Deserialize(new ArraySegment <byte>(session.Buffer, 0, session.Result.Count)); switch (m.Type) { case MessageType.Subscribe: this.Factory.AddSubscription(session, (Subscribe)m); break; case MessageType.GetSubscriptions: var list = this.Factory.GetSubscriptions(session); var response = new Subscriptions(); response.List = list; await session.SendAsync(response.Serialize()); break; default: await DoUnhandled(session, m); break; } } } catch { } }
async Task DoUnhandled(Session session, Message m) { Error error = new Error("Unhandled message type: " + m.Type); await session.SendAsync(error.Serialize()); }