コード例 #1
0
ファイル: Chat.razor.cs プロジェクト: DataJuggler/BlazorChat
        /// <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);
            }
        }