2015-02-11 78 views
1

我在试用ASP.Net Signalr的例子。在这个例子中,这是一个代码来改善集线器中的信号性能服务器端。这个代码在构造函数中有什么含义

这里是代码..

 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; } 
    } 

我有一个问题,什么是此行的意义..

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

它说,它通过在每个创建的构造函数中设置

这是什么意思?有没有其他的方式来写?

+1

http://stackoverflow.com/questions/4009013/call-one-constructor-from-another(读回答作为问题和问题作为答案) – Default 2015-02-11 09:30:38

+1

该评论似乎是针对该领域的,而不是构造函数。和'this(..)'只是调用另一个构造函数。这是你有问题的评论或“这个”电话吗?或“Broadcast.Instance”? – Default 2015-02-11 09:33:26

+0

你们是否每40ms就认真向所有客户进行广播?什么是用例? – Anders 2015-02-11 18:05:59

回答

2

此语法在您的类中调用另一个构造函数。 这意味着,像这样的电话:

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

会调用这个构造

public MoveShapeHub(Broadcaster broadcaster) 
相关问题