public List<string> GetSubscriptions(Session session) { List<string> list; if (perSessionSubs.TryGetValue(session, out list)) return list; return null; }
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 { } }
public void AddSubscription(Session session, Subscribe sub) { HashSet<Session> subs; if (!perUserSubs.TryGetValue(sub.User, out subs)) subs = perUserSubs[sub.User] = new HashSet<Session>(); lock (subs) { subs.Add(session); } List<string> list; if (!perSessionSubs.TryGetValue(session, out list)) list = perSessionSubs[session] = new List<string>(); list.Add(sub.User); }
public void Remove(Session session) { sessions.TryRemove(session.Id, out session); foreach (var u in perSessionSubs[session]) perUserSubs[u].Remove(session); }
public Session Create(WebSocket ws) { var session = new Session(this, ws); sessions[session.Id] = session; return session; }
async Task DoUnhandled(Session session, Message m) { Error error = new Error("Unhandled message type: " + m.Type); await session.SendAsync(error.Serialize()); }