Пример #1
0
        private void OnboardNewClient(NetPeer peer)
        {
            var channel  = new MessagingChannel(_messageTargetMap, peer);
            var clientId = Guid.NewGuid();

            var clientEntity = _world.CreateEntity();

            clientEntity.Set(new ClientMessagingTarget()
            {
                Channel = channel
            });
            clientEntity.Set(new Transform(clientEntity)
            {
                WorldPosition = new Vector3(0, 40, 0)
            });
            clientEntity.Set(new NetworkedEntity()
            {
                Id = clientId
            });
            clientEntity.Set(new Camera());
            clientEntity.Set(new EntityMetaData()
            {
                Name = "Player"
            });
            _world.Publish(new NewClientConnected(clientEntity));
            channel.SendBuffered();
        }
Пример #2
0
        public ClientSystem(World world, MessageTargetMap messageTargetMap, MessagingChannel messagingChannel)
        {
            _world = world;

            MessageListeners = new Dictionary <Type, IMessageReceiver>();

            _messageTargetMap = messageTargetMap;
            _messagingChannel = messagingChannel;

            var serverMessagingTargetEntity = _world.CreateEntity();

            serverMessagingTargetEntity.Set(new ServerMessagingTarget()
            {
                Channel = _messagingChannel
            });

            EventBasedNetListener listener = new EventBasedNetListener();

            _client = new NetManager(listener);
            _client.SimulatePacketLoss         = true;
            _client.SimulationPacketLossChance = 5;
            _client.DisconnectTimeout          = 60000;

            _client.Start();
            _client.Connect("localhost" /* host ip or name */, 9050 /* port */, "SomeConnectionKey" /* text key or NetDataWriter */);

            listener.PeerConnectedEvent += (netPeer) =>
            {
                _messagingChannel.PeerAdded(netPeer);
            };

            listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) =>
            {
                Utilties.Logging.Metrics.LogMetric("Client:Networking:RecievedMessages:Size", dataReader.UserDataSize, TimeSpan.FromSeconds(5));
                MessageRecieved(new ArraySegment <byte>(dataReader.RawData, dataReader.UserDataOffset, dataReader.UserDataSize));
                dataReader.Recycle();
            };
        }