예제 #1
0
        public Hub(IActorRef handler)
        {
            _handler = handler;

            Receive<Signal.Join>(join =>
            {
                if (!_clients.TryAdd(Sender, out _clients))
                    return;

                var joined = new Signal.Joined(Sender.Path.Name, Self.Path.Name);
                Self.Tell(new Signal.Broadcast("self", joined));

                _handler.Tell(joined);
            });

            Receive<Signal.Leave>(leave =>
            {
                if (!_clients.TryRemove(Sender, out _clients))
                    return;

                var left = new Signal.Left(Sender.Path.Name, Self.Path.Name);
                Self.Tell(new Signal.Broadcast("self", left));

                _handler.Tell(left);
            });

            Receive<Signal.Broadcast>(broadcast =>
            {
                var message = WriteObject(broadcast.Message);
                foreach (var client in _clients)
                    client.Tell(message);
            });

            ReceiveAny(o => Self.Forward(new Signal.Broadcast("self", o)));
        }
예제 #2
0
        public HubProxy(IActorRef handler)
        {
            _handler = handler;

            Receive<Signal.Join>(join =>
            {
                Sender.Tell(WriteObject(new Signal.Joined(Sender.Path.Name, Self.Path.Name, true)));

                //Client hasn't received the previous joined message so we don't need to forward it again to the other clients
                if (_clientDictionary.ContainsKey(Sender.Path.Name))
                    return;

                var joined = new Signal.Joined(Sender.Path.Name, Self.Path.Name);
                var joinedSerialized = WriteObject(joined);
                foreach (var client in _clientDictionary.Values)
                    client.Tell(joinedSerialized);

                _handler.Tell(joined);

                _clientDictionary[Sender.Path.Name] = Sender;
            });

            Receive<Signal.Leave>(leave =>
            {
                if (_clientDictionary.ContainsKey(Sender.Path.Name))
                    _clientDictionary.Remove(Sender.Path.Name);

                var left = new Signal.Left(Sender.Path.Name, Self.Path.Name);
                Self.Tell(new Signal.Broadcast("self", left));

                _handler.Tell(left);
            });

            Receive<Signal.Broadcast>(broadcast =>
            {
                var message = WriteObject(broadcast.Message);
                if (broadcast.ClientId == null)
                {
                    foreach (var client in _clientDictionary.Values)
                        client.Tell(message);
                }
                else if (_clientDictionary.ContainsKey(broadcast.ClientId))
                {
                    _clientDictionary[broadcast.ClientId].Tell(message);
                }
            });

            Receive<Signal.ClientBroadcast>(broadcast => _handler.Tell(broadcast));
        }