コード例 #1
0
        public static async void SendToAllClients(Message message, JsonMessage json, int roomID, string sender = "")
        {
            List <Task> tasks = new List <Task>();

            foreach (KeyValuePair <string, ClientInfo> network in clients)
            {
                if (sender != "")
                {
                    if (network.Key == sender)
                    {
                        continue;
                    }
                }
                if (roomID != -1)
                {
                    if (network.Value.RoomId != roomID)
                    {
                        continue;
                    }
                }
                if (message != null)
                {
                    if (message.Name == network.Key)
                    {
                        continue;
                    }
                }
                else
                {
                    if (json.Name == network.Key)
                    {
                        continue;
                    }
                }

                if (network.Value.ClientType == ClientType.Desktop)
                {
                    if (message == null)
                    {
                        tasks.Add(MessageHelpers.SetMessageAsync(network.Value.Stream, json.ToMessage()));
                    }
                    else
                    {
                        tasks.Add(MessageHelpers.SetMessageAsync(network.Value.Stream, message));
                    }
                }
                else if (network.Value.ClientType == ClientType.Web)
                {
                    if (json == null)
                    {
                        tasks.Add(JsonMessageHelpers.SetJsonMessageAsync(network.Value.Stream, message.ToJsonMessage()));
                    }
                    else
                    {
                        tasks.Add(JsonMessageHelpers.SetJsonMessageAsync(network.Value.Stream, json));
                    }
                }
                else
                {
                    Console.WriteLine("Unknown Client");
                }
            }

            while (tasks.Any())
            {
                tasks.Remove(await Task.WhenAny(tasks));
            }
        }