1

I'm trying the ASP.Net Signalr example. In the example here is a code to improve the signalr performance server side in the Hub.

Here is the Code..

        public class Broadcaster
        {
            private readonly static Lazy<Broadcaster> _instance =
                new Lazy<Broadcaster>(() => new Broadcaster());
            // We're going to broadcast to all clients a maximum of 25 times per second
            private readonly TimeSpan BroadcastInterval =
                TimeSpan.FromMilliseconds(40);
            private readonly IHubContext _hubContext;
            private Timer _broadcastLoop;
            private ShapeModel _model;
            private bool _modelUpdated;
            public Broadcaster()
            {
                // Save our hub context so we can easily use it 
                // to send to its connected clients
                _hubContext = GlobalHost.ConnectionManager.GetHubContext<MoveShapeHub>();
                _model = new ShapeModel();
                _modelUpdated = false;
                // Start the broadcast loop
                _broadcastLoop = new Timer(
                    BroadcastShape,
                    null,
                    BroadcastInterval,
                    BroadcastInterval);
            }
            public void BroadcastShape(object state)
            {
                // No need to send anything if our model hasn't changed
                if (_modelUpdated)
                {
                    // This is how we can access the Clients property 
                    // in a static hub method or outside of the hub entirely
                    _hubContext.Clients.AllExcept(_model.LastUpdatedBy).updateShape(_model);
                    _modelUpdated = false;
                }
            }
            public void UpdateShape(ShapeModel clientModel)
            {
                _model = clientModel;
                _modelUpdated = true;
            }
            public static Broadcaster Instance
            {
                get
                {
                    return _instance.Value;
                }
            }
        }

        public class MoveShapeHub : Hub
        {
            // Is set via the constructor on each creation
            private Broadcaster _broadcaster;
            public MoveShapeHub()
                : this(Broadcaster.Instance)
            {
            }
            public MoveShapeHub(Broadcaster broadcaster)
            {
                _broadcaster = broadcaster;
            }
            public void UpdateModel(ShapeModel clientModel)
            {
                clientModel.LastUpdatedBy = Context.ConnectionId;
                // Update the shape model within our broadcaster
                _broadcaster.UpdateShape(clientModel);
            }
        }




    public class ShapeModel
    {
        // We declare Left and Top as lowercase with 
        // JsonProperty to sync the client and server models
        [JsonProperty("left")]
        public double Left { get; set; }
        [JsonProperty("top")]
        public double Top { get; set; }
        // We don't want the client to get the "LastUpdatedBy" property
        [JsonIgnore]
        public string LastUpdatedBy { get; set; }
    }

I have a question that what is the meaning of this line..

 public MoveShapeHub()
                : this(Broadcaster.Instance)
            {
            }

It says its for set via the constructor on each creation

What does that mean? Is there any other way to write that???

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
  • 1
    http://stackoverflow.com/questions/4009013/call-one-constructor-from-another (read the answer as the question and the question as the answer) – default Feb 11 '15 at 09:30
  • 1
    the comment seems to be for the field, not the constructor. and `this(..)` is simply calling another constructor. Was it the comment you had questions about or the `this` call? or the `Broadcast.Instance`? – default Feb 11 '15 at 09:33
  • Are you guys seriously broadcasting to all clients each 40ms? Whats the use case? – Anders Feb 11 '15 at 18:05

1 Answers1

2

This syntax calls another constructor in your class. This means that a call like this:

public MoveShapeHub() : this(Broadcaster.Instance) {}

Will call this constructor

public MoveShapeHub(Broadcaster broadcaster)
Moti Azu
  • 5,392
  • 1
  • 23
  • 32