Introduction

This sample is the completed project created in the High-Frequency Realtime SignalR tutorial, located in the Getting Started section of the SignalR documentation (to be published soon).

Building the Sample

This solution requires NuGet packages to be downloaded. Visual Studio will download the necessary packages automatically. This solution is designed for Visual Studio 2012, including the Web Tools 2012.2 update.

Description

This solution contains a C# implementation of the SignalR Hub class as the server component, and an HTML page using JavaScript and JQuery as the client component.

The tutorial that demonstrates how to create this solution shows how to create a web application that uses ASP.NET SignalR to provide high-requency messaging functionality. High-frequency messaging in this case means up to 40 messages a second.

This application displays a shape that the users can drag. The position of the shape in all other browsers will then be updated to match the position of the dragged shape using timed updates.

Concepts introduced in the tutorial associated with this sample have applications in real-time gaming and other simulation applications. 

C#
Edit|Remove
using System; 
using System.Collections.Genericusing System.Linqusing System.Web; 
 
using System.Threadingusing Microsoft.AspNet.SignalR; 
 
using Newtonsoft.Json; 
 
namespace MoveShapeDemo 
{ 
    public class Broadcaster 
    { 
        private readonly static Lazy<Broadcaster>_instance =  
            new Lazy<Broadcaster>(() => new Broadcaster()); 
        // We're going to broadcast to all clients 50 times per second 
        private readonly TimeSpan BroadcastInterval =  
            TimeSpan.FromMilliseconds(20);  
        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 { getset; } 
 
        [JsonProperty("top")] 
        public double Top { getset; } 
 
        // We don't want the client to get the "LastUpdatedBy" property 
        [JsonIgnore] 
        public string LastUpdatedBy { getset; } 
    } 
     
}

Source Code Files