Send() public méthode

Queue a packet for sending to the specified client.
public Send ( MxClient client, byte packet, bool reliable = false ) : void
client MxClient /// The client to send the message to. ///
packet byte /// The associated data to send. ///
reliable bool /// Whether or not this message should be sent reliably and intact. This also /// permits messages larger than 512 bytes to be sent. ///
Résultat void
Exemple #1
0
        public static void Main(string[] args)
        {
            var dispatcher = new MxDispatcher(int.Parse(args[0]), int.Parse(args[0]) + 1);

            dispatcher.Connect(new DualIPEndPoint(IPAddress.Parse(args[1]), int.Parse(args[2]), int.Parse(args[2]) + 1));

            dispatcher.MessageSent += (sender, eventArgs) => Console.WriteLine("message sent " + Encoding.ASCII.GetString(eventArgs.Payload));
            dispatcher.MessageReceived += (sender, eventArgs) => Console.WriteLine("message received " + Encoding.ASCII.GetString(eventArgs.Payload));
            dispatcher.MessageLost += (sender, eventArgs) => Console.WriteLine("message lost " + Encoding.ASCII.GetString(eventArgs.Payload));
            dispatcher.MessageAcknowledged += (sender, eventArgs) => Console.WriteLine("message acknowledged " + Encoding.ASCII.GetString(eventArgs.Payload));

            var array = new[] { "hello", "world", "what", "is", "this", "i", "don't", "even" };
            var counter = 0;
            var accumulator = 0;

            while (true)
            {
                accumulator += 1;

                if (accumulator == 10)
                {
                    foreach (var endpoint in dispatcher.Endpoints)
                    {
                        dispatcher.Send(endpoint, Encoding.ASCII.GetBytes(array[counter]));
                    }

                    counter++;
                    if (counter >= array.Length)
                    {
                        counter = 0;
                    }

                    accumulator = 0;
                }

                dispatcher.Update();

                Thread.Sleep(1000 / 30);
            }
        }
Exemple #2
0
        public void Send <T>(MxDispatcher dispatcher, MxClientGroup target, T message, bool reliable = false)
        {
            if (target.RealtimeClients.Count == 0)
            {
                throw new InvalidOperationException(
                          "Attempted to send message to group " + target +
                          ", but it has no clients.");
            }

            var serialized = _networkMessageSerialization.Serialize(message);

            dispatcher.Send(target, serialized, reliable);

            var type = typeof(T);

            if (!_currentNetworkFrame.BytesSentByMessageType.ContainsKey(type))
            {
                _currentNetworkFrame.BytesSentByMessageType[type]    = 0;
                _currentNetworkFrame.MessagesSentByMessageType[type] = 0;
            }

            _currentNetworkFrame.BytesSentByMessageType[type] += serialized.Length;
            _currentNetworkFrame.MessagesSentByMessageType[type]++;
        }