Example #1
0
        /// <summary>
        /// retrieve new loaded channel
        /// </summary>
        public void createChannelReceived(Entities.NewChannelResponse m)
        {
            if (m.success)
            {
                l.log("nice channel creation: " + m.name);

                // очищаем поля (после успешного создания канала)
                w.ChannelIDBox.Text                  = "";
                w.ChannelNameBox.Text                = "";
                w.CrChBadData.Visibility             = Visibility.Hidden;
                w.CreateChannelParentGrid.Visibility = Visibility.Hidden;

                // отображаем новый канал
                w.ChannelList.Items.Add(createChannelGrid(m.fullname, 0, 1, m.name));
            }
            else
            {
                l.log("server response: bad channel creation: " + m.name);
                w.CrChBadData.Content                = "Ответ сервера: неверные данные";
                w.CrChBadData.Visibility             = Visibility.Visible;
                w.CreateChannelParentGrid.Visibility = Visibility.Visible;
            }
            // возвращаем управление
            w.sending = false;
        }
Example #2
0
        public static Entities.NewChannelResponse dynamicToNewChannelResponse(dynamic obj)
        {
            var ch = new Entities.NewChannelResponse();

            ch.name     = obj.name;
            ch.fullname = obj.fullname;
            ch.admin    = obj.admin;
            ch.success  = obj.success;
            ch.type     = obj.type;
            return(ch);
        }
Example #3
0
        /// <summary>
        /// initialise webSocket client with all configured listeners
        /// </summary>
        /// <returns>ws object</returns>
        private WebSocket initWebSocket()
        {
            WebSocket ws = new WebSocket(Config.wsSource);


            ws.OnMessage += (sender, e) =>
            {
                var resp = JsonConvert.DeserializeObject <dynamic>(e.Data);


                string type = resp.type;

                if ("authorize".Equals(type))
                {
                    bool success = resp.success;
                    l.log("new auth: " + success);
                    if (success)
                    {
                        l.log("success auth");
                        signinWindow.dispatchOpenMainWindow();
                    }
                    else
                    {
                        l.log("bad data for user");
                    }
                }

                // пришло новое сообщение - отображаем в списке сообщений
                else if ("message".Equals(type))
                {
                    l.log("new message");
                    Entities.MessageResponse m = Utils.dynamicToMessageResponse(resp);
                    while (mainWindow == null)
                    {
                        l.log("another sleep");
                        Thread.Sleep(100);
                    }
                    // show
                    Dispatchers.dispatchShowMessage(m, mainWindow);

                    // serialise
                    string channelName = m.channel;
                    var    ent         = new Entities.MessageEntity {
                        from = m.@from, message = m.message, time = m.time
                    };
                    using (var db = new LiteDatabase(@Config.userName + "_local.db"))
                    {
                        var messagesCollection = db.GetCollection <Entities.MessageEntity>(channelName + "_mes");
                        messagesCollection.Insert(ent);
                    }
                }

                else if ("register".Equals(type))
                {
                    l.log("registration answer");
                    bool success = resp.success;
                    if (success)
                    {
                        while (signupWindow == null)
                        {
                            l.log("another sleep");
                            Thread.Sleep(100);
                        }
                        signupWindow.dispatchOpenSigninWindow();
                    }
                    else
                    {
                        l.log("user already exists");
                    }
                }
                // рендерит в MainWindow список каналов
                else if ("get_channel".Equals(type))
                {
                    JArray         channels = resp.channels;
                    List <dynamic> chList   = channels.ToObject <List <dynamic> >();
                    l.log("received channels:  " + channels.ToString());

                    JArray       userCounts = resp.user_counts;
                    List <Int32> counts     = userCounts.ToObject <List <Int32> >();
                    l.log("received counts: " + userCounts.ToString());
                    Dispatchers.dispatchGetChannels(chList, counts, mainWindow);
                }

                else if ("new_channel".Equals(type))
                {
                    Entities.NewChannelResponse m = Utils.dynamicToNewChannelResponse(resp);
                    Dispatchers.dispatchCreateChannel(m, mainWindow);
                }
                else if ("get_channel_messages".Equals(type))
                {
                    JArray         array = resp.messages;
                    List <dynamic> ll    = array.ToObject <List <dynamic> >();
                    string         ch    = resp.channel;

                    l.log("received messages for channel " + ch + " are " + array.ToString());
                    // show
                    Dispatchers.dispatchShowChannelMessages(ch, ll, mainWindow);

                    // serialise
                    var entities = listDynamicToMessageEntities(ll);
                    try
                    {
                        using (var db = new LiteDatabase(Config.userName + "_local.db"))
                        {
                            var messagesCollection = db.GetCollection <Entities.MessageEntity>(ch + "_mes");
                            messagesCollection.EnsureIndex(x => x.time);
                            messagesCollection.Insert(entities);
                        }
                    }
                    catch (LiteException)
                    {
                        l.log("exception in serialise (invalid ch format probably)");
                    }
                }
                else if ("get_online_users".Equals(type))
                {
                    JArray array = resp.users;
                    Entities.GetOnlineUsers obj = new Entities.GetOnlineUsers();
                    obj.sender = resp.sender;
                    obj.users  = array.ToObject <List <string> >();
                    obj.type   = resp.type;
                    Dispatchers.dispatchGetOnlineUsers(obj, mainWindow);
                }

                else if ("add_user".Equals(type))
                {
                    Entities.AddUser evnt = new Entities.AddUser();
                    evnt.sender  = resp.sender;
                    evnt.user    = resp.user;
                    evnt.channel = resp.channel;
                    evnt.success = resp.success;
                    evnt.type    = resp.type;


                    if (evnt.sender == Config.userName)
                    {
                        // notify about successful or not adding
                        // display info about adding (using add user menu)
                        Dispatchers.dispatchNotifyOnAddingUserResponse(evnt.user, evnt.channel, evnt.success, mainWindow);
                    }
                    // evnt.success apriori true here
                    else
                    {
                        // user have been added
                        if (evnt.user == Config.userName)
                        {
                            // show new channel
                            mainWindow.globalChannel.generateChannelRequest(evnt.channel);
                        }
                        // other members of channel (exclude admin)
                        else
                        {
                            // update channel data
                            // +1 channel's members displayed
                            Dispatchers.dispatchIncrementChannelMembersView(evnt.channel, mainWindow);
                        }
                    }
                }
                else if ("get_channel_users".Equals(type))
                {
                    JArray array = resp.users;
                    Entities.GetChannelUsers obj = new Entities.GetChannelUsers();
                    obj.sender  = resp.sender;
                    obj.channel = resp.channel;
                    obj.users   = array.ToObject <List <Entities.User> >();
                    obj.type    = resp.type;
                    Dispatchers.dispatchGetChannelUsers(obj, mainWindow);
                }
            };
            // establish again
            ws.OnError += (sender, e) =>
            {
                l.log("error: " + e.Message);
                tryToConnect(ws);
            };

            tryToConnect(ws);
            return(ws);
        }
Example #4
0
 public static void dispatchCreateChannel(Entities.NewChannelResponse m, MainWindow w)
 {
     w.Dispatcher.BeginInvoke(new ThreadStart(delegate {
         w.globalChannel.createChannelReceived(m);
     }));
 }