Beispiel #1
0
        private void Conn_OnReceive(string data)
        {
            string  command = data.Split(':')[0];
            dynamic json    = JSON.Parse(data.Substring(data.IndexOf(':') + 1));

            switch ((string)json.type.ToString())
            {
            case "authenticate":
                if (json.success == true)
                {
                    foreach (var channel in session.GetAllChannels())
                    {
                        Send("!subscribe:messaging,threads." + channel.ThreadId);
                    }

                    foreach (var friendship in session.GetAllFriendships())
                    {
                        if (friendship.Active)
                        {
                            Send("!subscribe:messaging,users." + friendship.Addresser);
                        }
                    }

                    Console.WriteLine("Authenticated !");
                }
                else
                {
                    Console.WriteLine("Oops.. check your credentitals..");
                }
                break;

            case "subscribe":
                if (json.success == true)
                {
                    Console.WriteLine("Subscribed to " + json.payload.channel);
                }
                else
                {
                    Console.WriteLine("Unable to subscribe to " + json.payload.channel);
                }
                break;

            case "discussion_change":
                OnDiscussionChange?.Invoke(Parser.JSONtoEntity(json.payload, typeof(PrivateDiscussion)));
                break;

            case "channel_change":
                OnChannelChange?.Invoke(Parser.JSONtoEntity(json.payload, typeof(Channel)));
                break;

            case "new_friend_request":
                OnNewFriendshipRequest?.Invoke(Parser.JSONtoEntity(json.payload, typeof(FriendRequest)));
                break;

            case "friendship_addressed":
                OnFriendshipAdressed?.Invoke(Parser.JSONtoEntity(json.payload, typeof(Friendship)));
                break;

            case "message":
                OnMessageReceived?.Invoke(Parser.JSONtoEntity(json.payload, typeof(Message)));
                break;

            default:
                Console.WriteLine("Unhandled event " + json.type);
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Évènement déclenché lors de la réception de données du serveur
        /// </summary>
        /// <param name="data">Données venant du serveur</param>
        private void Conn_OnReceive(string data)
        {
            string  command = data.Split(':')[0];
            dynamic json    = JSON.Parse(data.Substring(data.IndexOf(':') + 1));

            Console.WriteLine(data);
            switch ((string)json.type.ToString())
            {
            case "authenticate":
                if (json.success == true)
                {
                    Send("!subscribe:messaging,users." + session.GetProfile().UserId);

                    foreach (var c in session.GetAllChannels())
                    {
                        Send("!subscribe:messaging,threads." + c.ThreadId);
                        subscriptions.Add(c.ThreadId, c);
                    }

                    foreach (var p in session.GetAllPrivateDiscussions())
                    {
                        Send("!subscribe:messaging,threads." + p.ThreadId);
                        subscriptions.Add(p.ThreadId, p);
                    }


                    Console.WriteLine("Authenticated !");
                }
                else
                {
                    Console.WriteLine("Oops.. check your credentitals..");
                }
                break;

            case "subscribe":
                if (json.success == true)
                {
                    Console.WriteLine("Subscribed to " + json.payload.channel);
                }
                else
                {
                    Console.WriteLine("Unable to subscribe to " + json.payload.channel);
                    subscriptions.Remove(Convert.ToInt32(((string)json.payload.channel).Split('.')[1]));
                }
                break;

            case "discussion_change":
                PrivateDiscussion discussion = Parser.JSONtoEntity(json.payload, typeof(PrivateDiscussion));
                if (!subscriptions.ContainsKey(discussion.ThreadId))
                {
                    subscriptions.Add(discussion.ThreadId, discussion);
                    Send("!subscribe:messaging,threads." + discussion.ThreadId);
                }

                OnDiscussionChange?.Invoke(Parser.JSONtoEntity(json.payload, typeof(PrivateDiscussion)));
                break;

            case "channel_change":
                Channel channel = Parser.JSONtoEntity(json.payload, typeof(Channel));
                if (!subscriptions.ContainsKey(channel.ThreadId))
                {
                    subscriptions.Add(channel.ThreadId, channel);
                    Send("!subscribe:messaging,threads." + channel.ThreadId);
                }

                OnChannelChange?.Invoke(channel);
                break;

            case "new_friend_request":
                OnNewFriendshipRequest?.Invoke(Parser.JSONtoEntity(json.payload, typeof(FriendRequest)));
                break;

            case "friendship_addressed":
                OnFriendshipAdressed?.Invoke(Parser.JSONtoEntity(json.payload, typeof(Friendship)));
                break;

            case "message":
                OnMessageReceived?.Invoke(Parser.JSONtoEntity(json.payload, typeof(Message)));
                break;

            default:
                Console.WriteLine("Unhandled event " + json.type);
                break;
            }
        }