14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

while ((line = Console.ReadLine()) != null)<br />

{<br />

if (int.TryParse(line, out customerId))<br />

{<br />

var customer = await myHub.Invoke(“Get”,<br />

customerId);<br />

if (customer == null) Console.WriteLine(“No customer<br />

found”);<br />

else Console.WriteLine(“Server found a customer with<br />

name {0}”, customer.Name);<br />

}<br />

}<br />

}<br />

the web interface, we see that the console application is notified<br />

when a new customer is added and then prints the amount of<br />

customers. The count is taken from the parameter that is sent to<br />

the action while the data is passed all the way from the SignalR<br />

hub.<br />

Because marking void methods asynchronous is a very bad<br />

practice, we’ve set it to return a task so that we can follow the<br />

operation if we’d like to. The method will read a line from the<br />

command line and try to parse it as an integer, then it will either<br />

tell us the customer name or tell us that there are no customers<br />

at all.<br />

Fire up both the console application and the website and first<br />

ask for a customer before you’ve added a customer, then add a<br />

customer in the web interface and ask again as you can see in the<br />

Figure below:<br />

Figure 8<br />

Connecting to the Hub in WinRT using HTML & JavaScript<br />

We’ve seen how to connect to hubs using a web based<br />

client as well as a normal .NET application running in Windows,<br />

but what about Windows 8, more specifically WinRT<br />

Unfortunately it’s not as simple as just including the JavaScript<br />

on the server in a WinRT application because WinRT Applications<br />

don’t allow loading external resources. But it’s not too difficult<br />

either.<br />

We’d also like the console application to subscribe to the event<br />

where the client is notified when a customer is added. This is<br />

easily done through the hub proxy just as we did with the<br />

invocation of a method, but this time we use the method called<br />

On instead. This method defines that we are listening for a certain<br />

message. The first parameter is the message that we are<br />

listening to and the second is the action to run when the message<br />

is received.<br />

This needs to be added before we start the connection and the<br />

code looks like this:<br />

myHub.On(“RefreshCustomers”, (customers) => {<br />

Console.WriteLine(“New customers added, total amount<br />

of customers {0}”, customers.Count);<br />

});<br />

If we run this as you see in Figure 8 and add some customers in<br />

First off create a new blank JavaScript<br />

Windows Store application, you can call this<br />

MyWinRTHubClient. We can’t simply install<br />

SignalR using NuGet this time, instead we<br />

need to manually add some necessary files.<br />

We need the following files for it to work,<br />

these can be copied from the server project<br />

we created earlier:<br />

• jquery.signalR-1.0.1.min.js<br />

• jquery-1.6.4.min.js<br />

Copy these files over to the js directory in the Windows Store<br />

project and reference them in the default HTML file created for<br />

the project. The JavaScript that we’re adding is really not that<br />

different from what we saw in the normal web-browser client; we<br />

can even use the same HTML!<br />

When the document has finished loading, we want to create a hub<br />

connection and create a proxy for the SignalR server. This looks<br />

similar to what we saw before, but notice that it’s not entirely the<br />

same:<br />

var connection = $.hubConnection(‘http://localhost:3448/’);<br />

DNcmagazine www.dotnetcurry.com | 21

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

Saved successfully!

Ooh no, something went wrong!