Esempio n. 1
0
 public List<string> GetSubscriptions(Session session)
 {
     List<string> list;
     if (perSessionSubs.TryGetValue(session, out list))
         return list;
     return null;
 }
Esempio n. 2
0
        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 { }
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
 public void Remove(Session session)
 {
     sessions.TryRemove(session.Id, out session);
     foreach (var u in perSessionSubs[session])
         perUserSubs[u].Remove(session);
 }
Esempio n. 5
0
 public Session Create(WebSocket ws)
 {
     var session = new Session(this, ws);
     sessions[session.Id] = session;
     return session;
 }
Esempio n. 6
0
 async Task DoUnhandled(Session session, Message m)
 {
     Error error = new Error("Unhandled message type: " + m.Type);
     await session.SendAsync(error.Serialize());
 }