public static void sendGetChannelUsersRequest(string channel, WsController c) { var ws = c.getWs(); if (ws != null) { l.log("sending get channel users request"); Entities.GetChannelUsers req = new Entities.GetChannelUsers(); req.sender = Config.userName; req.type = "get_channel_users"; req.channel = channel; ws.Send(JsonConvert.SerializeObject(req)); } }
public void getChannelUsersReceived(Entities.GetChannelUsers obj) { List <Entities.User> users = obj.users; w.ChannelUsersList.Items.Clear(); foreach (Entities.User user in users) { TextBlock l = new TextBlock(); if (user.type != null) { l.Text = user.login + " (" + user.type + ")"; } else { l.Text = user.login; } w.ChannelUsersList.Items.Add(l); } w.GetChannelUsers.Visibility = Visibility.Visible; }
/// <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); }
public static void dispatchGetChannelUsers(Entities.GetChannelUsers obj, MainWindow w) { w.Dispatcher.BeginInvoke(new ThreadStart(delegate { w.globalChannel.getChannelUsersReceived(obj); })); }