14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Connecting to the Hub<br />

We can try this out in the same application as we have the server<br />

running, by adding a new HTML page for adding and displaying<br />

customers. Add a file called Client.html and add the following to<br />

the header section to include the SignalR generated JavaScript,<br />

jQuery and the SignalR common JavaScript:<br />

<br />

<br />

<br />

<br />

The actual form will look very simple, it will only have two fields<br />

and two buttons like you can see below in figure 5<br />

myHub.server.all().done(function (result) {<br />

var resultDiv = $(“#result”);<br />

resultDiv.html(“”);<br />

resultDiv.append(“”);<br />

$(result).each(function (index, item) {<br />

resultDiv.append(“(“+item.Id+”) “+ item.Name +<br />

””);<br />

});<br />

resultDiv.append(“”);<br />

})<br />

});<br />

This hooks up a click handler to the refresh button and if we have<br />

any elements in the customer array, it will be displayed nicely on<br />

the page. In order to test that, we need to add some elements. This<br />

can be done in an equal manner by running the add() method we<br />

created before and passing it a JSON object like this:<br />

The HTML itself is equally simple:<br />

<br />

Id:<br />

Name: <br />

<br />

<br />

<br />

<br />

<br />

$(“#add”).click(function () {<br />

myHub.server.add({ id: $(“#id”).val(), name: $(“#name”).<br />

val() });<br />

});<br />

Last but not least, we can initiate the connection to the server:<br />

$.connection.hub.start();<br />

Run this and ‘Add’ some names and click on Refresh. This will give<br />

us an archaic looking list as we can see in Figure 6.<br />

When the document has finished loading, we want to hook up<br />

SignalR and perform some magic. First we get a reference to<br />

myHubServer through the $.connection object like this:<br />

var myHub = $.connection.myHubServer;<br />

We can then use myHub to send and receive messages from the<br />

server. For instance, in order to call all() we simply do this:<br />

myHub.server.all();<br />

There’s a common pattern here so we can append the method<br />

done() after our calls, meaning that once all is finished, we can<br />

execute a method with the result data. To do this and then add a<br />

new list with the items returned from the server to the page, we<br />

can simply do the following:<br />

$(“#refresh”).click(function () {<br />

Notice what happens if you open up a new tab in your browser,<br />

navigate to the same html file and hit refresh, you’ll get a list of<br />

all the names! Aside from the network latency and other factors,<br />

this is so far a one-way-push-real-time network application!<br />

What about if we want to notify the clients once a new customer<br />

has been added and refresh automatically We could do this quite<br />

easily. Let’s start off by adding an event handler on the client side<br />

called refreshCustomers like this:<br />

myHub.client.refreshCustomers = function (result) {<br />

// Same code here as refresh click handler<br />

};<br />

DNcmagazine www.dotnetcurry.com | 19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!