Example #1
0
 public static void sendAddUserRequest(string user, string channel, string fullname, WsController c)
 {
     Entities.AddUser req = new Entities.AddUser();
     req.sender   = Config.userName;
     req.user     = user;
     req.channel  = channel;
     req.fullname = fullname;
     req.type     = "add_user";
     l.log("try send add user " + user + " to " + channel + " request");
     if (Utils.sendRequest(req, c))
     {
         l.log("sended");
     }
     else
     {
         l.log("sending aborted");
     }
 }
Example #2
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);
        }