private void Form1_Load(object sender, EventArgs e) { pubnub.AddListener(new SubscribeCallbackExt( (pubnubObj, message) => { Console.WriteLine("message" + message); // Handle new message stored in message.Message if (message != null) { if (message.Channel != null) { Invoke(new Action(() => { //List<Message> iMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(message.ToString()).Cast<Message>().ToList(); //var lMessage = JsonConvert.DeserializeObject<Message>(pubnub.JsonPluggableLibrary.SerializeToJsonString(message)); // Message has been received on channel group stored in // message.Channel() var jMessage = JObject.Parse(pubnub.JsonPluggableLibrary.SerializeToJsonString(message.Message)); sb.FontStyle(FontStyle.Bold) .ForeColor(KnownColor.DarkGreen) .Append($"<{Helpers.TimeTokenToDateTime(message.Timetoken)}> ") .FontStyle(FontStyle.Regular); //rtbChat.Rtf += sb.ToString(); if (jMessage["username"].ToString() == _username) { sb.ForeColor(KnownColor.DarkRed).Append($"<{jMessage["username"]}>: "); } else { sb.ForeColor(KnownColor.DarkBlue).FontStyle(FontStyle.Bold).Append($"<{jMessage["username"]}>: ").FontStyle(FontStyle.Regular); if (WindowState == FormWindowState.Minimized) { Helpers.Notification($"Kappa - New Message of {jMessage["username"]}", $"<{jMessage["username"]}>: {jMessage["msg"]}", Properties.Resources.new_message); FlashWindowHelper.Flash(this); } } sb.ForeColor(KnownColor.Black).FontStyle(FontStyle.Bold) .AppendLine($"{jMessage["msg"].ToString()}"); rtbChat.Rtf = Emoticon.Parse(sb.ToString(), ref rtbChat); scrollToBottom(); })); } /* * else * { * Invoke(new Action(() => * { * // Message has been received on channel stored in * // message.Subscription() * tbOutput.AppendText("message.Subscription " + pubnub.JsonPluggableLibrary.SerializeToJsonString(message)); * })); * } */ /* * log the following items with your favorite logger * - message.Message() * - message.Subscription() * - message.Timetoken() */ } }, (pubnubObj, presence) => { Invoke(new Action(() => { var timestamp = Helpers.UnixTimeStampToDateTime(presence.Timestamp).ToString("dd/MM/yyyy HH:mm"); if (presence == null) { return; } if (presence.State["username"] == null) { return; } if (presence.State["username"].ToString() == _username) { return; } sb.FontStyle(FontStyle.Bold) .ForeColor(KnownColor.DarkGreen) .Append($"<{timestamp}> ") .FontStyle(FontStyle.Bold) .ForeColor(KnownColor.LimeGreen); switch (presence.Event) { case "join": sb.AppendLine($"{presence.State["username"]} joined the chat! tell him hi!!"); break; case "leave": sb.AppendLine($"{presence.State["username"]} Left the chat, we hope you come back!"); break; case "timeout": sb.AppendLine($"{presence.State["username"]} Fell, we hope you come back!"); break; case "state-change": sb.AppendLine($"{presence.State["username"]} Joined the chat! tell him hi!"); break; } rtbChat.Rtf = Emoticon.Parse(sb.ToString(), ref rtbChat); scrollToBottom(); })); }, (pubnubObj, status) => { if (status.Category == PNStatusCategory.PNUnexpectedDisconnectCategory) { // This event happens when radio / connectivity is lost Invoke(new Action(() => { tbOutput.AppendText("Connection Lost "); Helpers.Notification(Configuration.NOTIFICATION_TITLE, "Connection Lost!", Properties.Resources.error); lbStatus.Text = "Connection Lost!"; button1.Enabled = false; tbMessage.Enabled = false; tHereNow.Stop(); })); } else if (status.Category == PNStatusCategory.PNConnectedCategory) { Invoke(new Action(() => { // Connect event. You can do stuff like publish, and know you'll get it. // Or just use the connected event to confirm you are subscribed for // UI / internal notifications, etc tbOutput.AppendText("Connected! "); Helpers.Notification(Configuration.NOTIFICATION_TITLE, "Connected! \nEnjoy Made with ❤ 13dev.", Properties.Resources.info); lbStatus.Text = "Connected!"; this.Width = 755; button1.Enabled = true; tbMessage.Enabled = true; rtbChat.ResetText(); Dictionary <string, object> data = new Dictionary <string, object>(); data.Add("username", _username); pubnub.SetPresenceState() .Channels(Configuration.CHANNELS) .State(data) .Async(new PNSetStateResultExt( (r, s) => { Invoke(new Action(() => { tbOutput.AppendText(pubnub.JsonPluggableLibrary.SerializeToJsonString(r)); })); })); //Actualize the info tHereNow.Start(); tHereNow_Tick(null, EventArgs.Empty); //call tick history(); })); } else if (status.Category == PNStatusCategory.PNReconnectedCategory) { Invoke(new Action(() => { // Happens as part of our regular operation. This event happens when // radio / connectivity is lost, then regained. tbOutput.AppendText("Reconneted"); lbStatus.Text = "Reconneted!"; Helpers.Notification(Configuration.NOTIFICATION_TITLE, "Reconnected!", Properties.Resources.info); button1.Enabled = true; tbMessage.Enabled = true; tHereNow.Start(); })); } else if (status.Category == PNStatusCategory.PNDecryptionErrorCategory) { Invoke(new Action(() => { // Handle messsage decryption error. Probably client configured to // encrypt messages and on live data feed it received plain text. tbOutput.AppendText("messsage decryption error"); lbStatus.Text = "messsage decryption error!"; Helpers.Notification(Configuration.NOTIFICATION_TITLE, "Messsage decryption error!", Properties.Resources.error); })); } } )); if (String.IsNullOrEmpty(_username)) { Environment.Exit(0); } subscribe(); }
private void history() { pubnub.History() .Channel(Configuration.CHANNELS[0]) .Reverse(false) .Count(50) .IncludeTimetoken(true) .Async(new PNHistoryResultExt( (r, s) => { Invoke(new Action(() => { if (String.IsNullOrEmpty(r.Messages.ToString().Trim())) { sb.Font(RTFFont.LucidaConsole).FontSize(22) .FontStyle(FontStyle.Bold) .ForeColor(KnownColor.Red) .AppendLine($"No messages."); } //for each on messages foreach (var message in r.Messages) { if (message.Entry == null || message.Entry.ToString() == "") { continue; } //try parse json try { var jMessage = JObject.Parse(pubnub.JsonPluggableLibrary.SerializeToJsonString(message.Entry)); if (jMessage["username"] == null || jMessage["msg"] == null) { continue; } if (String.IsNullOrEmpty(jMessage["username"].ToString()) || String.IsNullOrEmpty(jMessage["msg"].ToString())) { continue; } //rtbChat.AppendText(Environment.NewLine); // if is first element if (r.Messages.First() == message) { sb.Font(RTFFont.LucidaConsole).FontSize(22) .FontStyle(FontStyle.Bold) .ForeColor(KnownColor.Green) .AppendLine($"Started in: { Helpers.TimeTokenToDateTime(message.Timetoken)}"); } sb.Font(RTFFont.LucidaConsole).FontSize(22) .FontStyle(FontStyle.Regular) .ForeColor(KnownColor.DarkGray) .Append($"<{Helpers.TimeTokenToDateTime(message.Timetoken)}> "); //rtbChat.Rtf += sb.ToString(); if (jMessage["username"].ToString() == _username) { sb.Font(RTFFont.LucidaConsole).FontSize(22).ForeColor(KnownColor.DarkSlateGray).Append($"<{jMessage["username"]}>: "); } else { sb.Font(RTFFont.LucidaConsole).FontSize(22).ForeColor(KnownColor.Black).Append($"<{jMessage["username"]}>: "); } sb.Font(RTFFont.LucidaConsole).FontSize(22).ForeColor(KnownColor.Gray) .AppendLine($"{jMessage["msg"]}"); } catch (JsonReaderException e) { tbOutput.AppendText(Environment.NewLine); tbOutput.AppendText("JsonReaderException: " + e.Message); //next one continue; } } // Parse emojis rtbChat.Rtf = Emoticon.Parse(sb.ToString(), ref rtbChat); //scrool to bottom scrollToBottom(); })); })); }