Exemple #1
0
        public void readMessageFromTextBoxAndSendIt()
        {
            if (w.MessageTextBox.Text != "")
            {
                var ws = w.wsController.getWs();
                if (ws != null)
                {
                    // берем имя выбранного канала
                    Grid   ch   = (Grid)w.ChannelList.SelectedItems[0];
                    string name = ((TextBlock)ch.Children[3]).Text;

                    string time = Utils.getCurrentTime();
                    // show
                    w.MessageList.Items.Add(createMyMessageGrid(w.MessageTextBox.Text, time));
                    // send
                    Entities.MessageResponse mes = new Entities.MessageResponse("message", w.MessageTextBox.Text, Config.userName, DateTimeOffset.Now.ToUnixTimeMilliseconds(), name);
                    string jsonReq = JsonConvert.SerializeObject(mes);
                    l.log("sending message");
                    ws.Send(jsonReq);

                    w.MessageTextBox.Text = "";
                    ScrollMessageListToEnd();

                    // serialise
                    using (var db = new LiteDatabase(@Config.userName + "_local.db"))
                    {
                        var messages = db.GetCollection <Entities.MessageEntity>(mes.channel + "_mes");
                        var ent      = new Entities.MessageEntity {
                            from = mes.@from, message = mes.message, time = mes.time
                        };
                        messages.Insert(ent);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// show new message (one) from server for current channel
        /// </summary>
        public void showMessageReceived(Entities.MessageResponse mes)
        {
            Grid   ch   = (Grid)w.ChannelList.SelectedItems[0];
            string name = ((TextBlock)ch.Children[3]).Text;

            if (mes.channel == name)
            {
                Grid mGrid;
                if ([email protected](Config.userName))
                {
                    mGrid = createMyMessageGrid(mes.message, Utils.getCurrentTime());
                }
                else
                {
                    mGrid = createAnotherMessageGrid(mes.@from, mes.message, Utils.getCurrentTime());
                }
                w.MessageList.Items.Add(mGrid);
                ScrollMessageListToEnd();
            }
        }
        /// <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);
        }
Exemple #4
0
 public static void dispatchShowMessage(Entities.MessageResponse mes, MainWindow w)
 {
     w.Dispatcher.BeginInvoke(new ThreadStart(delegate {
         w.globalMessages.showMessageReceived(mes);
     }));
 }