/// <summary> /// This method Send Message /// </summary> public void SendMessage(bool isPrivate = false, SubscriberCallback subscriber = null) { // Create a new instance of a 'SubscriberMessage' object. SubscriberMessage message = null; // Create a new instance of a 'SubscriberMessage' object. message = new SubscriberMessage(); message.Text = MessageText; message.FromId = Id; message.FromName = SubscriberName; message.SentTime = DateTime.Now; // Set the Time message.Sent = DateTime.Now; // Set the message.BubbleColor = (BubbleColorEnum)Shuffler.PullNextItem(); try { // If the MessageText string exists if (TextHelper.Exists(MessageText)) { // if this is a private message if ((isPrivate) && (NullHelper.Exists(subscriber)) && (subscriber.HasCallback)) { // Set the ToName message.ToName = subscriber.Name; message.ToId = subscriber.Id; // This is a private message message.IsPrivate = true; // Send this message to all clients SubscriberService.SendPrivateMessage(subscriber, message); } else { // Set the ToName message.ToName = "Room"; message.ToId = Guid.Empty; // Send this message to all clients SubscriberService.BroadcastMessage(message); } // Erase the Text MessageText = ""; // Deliver the message to this client, without being Broadcast Listen(message); } } catch (Exception error) { // for debugging only DebugHelper.WriteDebugError("BroadCastMessage", "Chat.razor.cs", error); } }
/// <summary> /// This event registers with the chat server /// </summary> public void RegisterWithServer() { SubscriberCallback callback = new SubscriberCallback(SubscriberName); callback.Callback = Listen; callback.Name = SubscriberName; // Get a message back SubscriberMessage message = SubscriberService.Subscribe(callback); // if message.Text exists and equals Subscribed if ((NullHelper.Exists(message)) && (message.HasText) && (TextHelper.IsEqual(message.Text, "Subscribed"))) { // Set to true Connected = true; // Set the Id the Server assigned this.Id = message.ToId; } // Convert the Subscribers to Names this.Names = SubscriberService.GetSubscriberNames(); // get the count int count = NumericHelper.ParseInteger(message.Data.ToString(), 0, -1); // if there are two people online or more if (count > 1) { // send a message to everyone else this user has joined SubscriberMessage newMessage = new SubscriberMessage(); // set the text newMessage.FromId = Id; newMessage.FromName = SubscriberName; newMessage.Text = SubscriberName + " has joined the conversation."; newMessage.ToId = Guid.Empty; newMessage.ToName = "Room"; newMessage.IsSystemMessage = true; // Send the message SubscriberService.BroadcastMessage(newMessage); // 6.5.2020: Get the Messages as you connect now this.Messages = SubscriberService.GetBroadcastMessages(this.Id, DisplayMessagesCount); } // Update Refresh(); }