Let’s discuss how we’d implement the application in Figure 1, which allows one browser to broadcast a drag operation to other browsers. This means that if a user drags a shape in one browser, all other browsers will see the shape move on their own browser. If Browser #1 moves the grey shape, the shape will automatically move in Browser #2.
Exercise 1: Task 2 - Use NuGet to Install SignalR NuGet is probably the easiest way to work with SignalR. It allows you to easily download the SignalR assemblies, and add some SignalR sample source code to your project. It also adds the needed references.
using System; using System.Collections.Generic; using System.Linq; using System.Web; // Add reference using SignalR.Hubs; namespace CloudSignalRSample_WebRole { // Derive from Hub, which is a server-side class // and a client side proxy. [HubName("moveShape")] // moveShape is the name used in the Javascript public class MoveShapeHub : Hub { public void MoveShape(int x, int y) {
// Broadcast drag event to other browsers
Clients.shapeMoved(Context.ConnectionId, x, y); // Simple diagnostics for debugging System.Diagnostics.Debug.WriteLine("x = " + x + ", y = " + y); } } }
/// <reference path="Scripts/jquery-1.6.4.js" /> /// <reference path="Scripts/jquery.signalR.js" /> /// <reference path="Scripts/jquery-ui-1.8.18.js" /> $(function () { // Get a refeerence to the server-side moveShape() class. var hub = $.connection.moveShape, // Get a reference to the shape div in the html $shape = $("#shape"); // Use extend to move the shape object (if we are not the sender) $.extend(hub, { // Use css to move the shape object shapeMoved: function (cid, x, y) { if ($.connection.hub.id !== cid) { $shape.css({ left: x, top: y }); $("p:last").text("left: " + x + ", top: " + y); } } }); // Wire up the draggable behavior (when hub is done starting) // "done" is a jquery deferred method $.connection.hub.start().done(function () { $shape.draggable({ // Implement draggable effect for jquery drag: function () { // Tell the server that the shape was just dragged hub.moveShape(this.offsetLeft, this.offsetTop); } }); }) });
<!DOCTYPE html > <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> #shape { width: 200px; height: 200px; background: #ccc; border: 2px solid #333; } p { margin-left:10px; } </style> </head> <body> <div id="shape"></div> <script src="../Scripts/jquery-1.6.4.js" type="text/javascript"></script> <script src="../Scripts/jquery-ui-1.8.18.js" type="text/javascript"></script> <script src="../Scripts/jquery.signalR.js" type="text/javascript"></script> <script src="/signalr/hubs" type="text/javascript"></script> <script src="MoveShape.js" type="text/javascript"></script> <div> <p>Hello</p></div><p></p> </body> </html>
Bruno: Great post! I would be interested in anything related to scalability testing of this type of solution to see where the scale-out metrics show.
This is a well written tutorial but this approach will only work properly with a single instance as you are still using the default MessageBus. Anyone hoping to scaleout SignalR will have to wait for the 0.5 release and use the Azure ServiceBus, Redis or Sql MessageBus implementations.