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.

Now we can start adding things to the server. Open up MyServer<br />

again and override a method called OnConnected. This method<br />

will be invoked each time a client connects to the server. These<br />

methods will all return Task so you can work with this in an<br />

asynchronous manner.<br />

The first parameter gives us information about the current request<br />

such as the user, data posted and things like that. The second<br />

parameter identifies the connection based on a connection id<br />

(Guid). This id can be used to direct messages to the client.<br />

WriteLine(data); };<br />

myServer.Start();<br />

You might need to change the port for your SignalR server. Once<br />

you’ve done that, fire up a couple of instances of this application.<br />

As you can see, the first one will get as many greetings as the<br />

number of clients. This is because we are using broadcasting to<br />

send the message to all the clients, as you can see below<br />

There’s a property in PersistentConnection called Connection that<br />

we can use to either broadcast or send data to all or one client. If<br />

we want to broadcast to everyone except certain clients, we can<br />

do this as well because Broadcast takes a second parameter which<br />

tells us which clients to exclude.<br />

In order to broadcast a message to each client that connects, we<br />

do the following:<br />

protected override Task OnConnected(IRequest request,<br />

string<br />

connectionId)<br />

{<br />

return Connection.Broadcast(“Hello there!”);<br />

}<br />

The same goes for sending a message to a specific client except<br />

Send takes another parameter that defines what client is<br />

receiving the message. As you might understand, this is quite low<br />

level, and there’s no protocol defined for how the messages will<br />

look or anything like that. You’ll need to figure that out yourself<br />

when using persistent connections. So far, it’s still a lot nicer than<br />

working directly with sockets. It’s time to start the project, as you<br />

might see it will throw an error because your web browser does<br />

not speak the language the server understands, so just ignore this<br />

for now.<br />

Connection to the server<br />

To test if the message really is broadcasted to all clients, we need<br />

to setup a couple of clients. Create a new console application<br />

called MyClient and fire up the package management console<br />

again and write the following:<br />

Install-Package Microsoft.AspNet.SignalR.Client<br />

Now open up Program.cs and add the following to your main<br />

method:<br />

You’ll notice that each message is sent instantly to each client<br />

making it feel very close to real-time. Remember though that this<br />

is on a local system and that we do indeed have some overhead in<br />

a real-world scenario.<br />

Persistent connections give you great power but you’re not getting<br />

much more from the framework other than a nice abstraction on<br />

top of the underlying transportation. As mentioned previously,<br />

SignalR will detect which transportation is most suitable and use<br />

that.<br />

Hubs<br />

If you’re not too comfortable working with something raw as<br />

persistent connection, you really don’t have to worry. With SignalR<br />

comes another abstraction called Hubs. This is an abstraction on<br />

top of Persistent Connections which will make everything a lot<br />

easier. If it wasn’t already easy enough, right<br />

With Persistent Connections and larger applications, you might<br />

imagine that you’d have to create a message loop which<br />

translates the data sent to the server and calls the correct<br />

delegate for that event. With Hubs, this is completely done for you.<br />

You can simply create methods that you register to certain events<br />

and are raised by clients.<br />

To set it up, create a new empty web application project called<br />

HubsServer and install SignalR into it using the package<br />

management console as you saw before.<br />

var myServer = new Connection(“http://localhost:8082/”);<br />

myServer.Received += (string data) => { Console.<br />

Create a new class called MyHubServer and inherit from Hub. This<br />

will let you, pretty much like we saw with Persistent Connection,<br />

DNcmagazine www.dotnetcurry.com | 17

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

Saved successfully!

Ooh no, something went wrong!