Melanjutkan tulisan saya sebelumnya Pengenalan SignalR, pada tulisan kali ini saya berikan contoh sederhana penggunaan singalR. Contoh ini diambil dari kursus Microsoft virtual academy "Building apps with ASP.NET 4.5". Pada tutorial ini Anda membutuhkan paket NuGet untuk mengunduh. Visual Studio akan mengunduh paket yang dibutuhkan secara otomatis. Contoh ini didesain untuk Visual Studio 2012.
Aplikasi ini menampilkan sebuah obyek div yang mana pengguna bisa menggesernya. Letak obyek yang ditampilkan di semua peramban lain akan diupdate sesuai dengan posisi obyek yang digeser.
Sebagai komponen server, halaman web menerapkan kelas SignalR Hub menggunakan C#, pada sisi klien menggunakan HTML dan jQuery.
Konsep ini digunakan dalam aplikasi Game real-time dan aplikasi simulasi lainnya.
Index.html
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
head
title
>SignalR Example</
#shape {
width: 100px;
height: 100px;
background-color: red;
cursor:move;
}
#count {
font-size:60px;
color:lightGray;
float:right;
</
body
div
id
"count"
>0</
"shape"
></
script
src
"Scripts/jquery-1.6.4.js"
"Scripts/jquery-ui-1.10.3.js"
"Scripts/jquery.signalR-1.1.3.js"
"/signalr/hubs"
> <!--this is for automatically
generate a javascript proxy class for our server class-->
$(function () {
var hub = $.connection.moveShape, //set hub with the server side class
$shape = $("#shape");
hub.client.usersConnected = function (numUsers) { //this instanciate the
usersConnected function receiving the numUsers parameter which is the number of users connected
$("#count").text(numUsers); //show the number of users connected inside a div
};
hub.client.shapeMoved = function (x, y) { //this instanciate the shapeMoved function
receiving x, y
$shape.css({ left: x, top: y }); //this moves the shape in the clients to
the coords x, y
$.connection.hub.start().done(function () {//when the connection is ready,
we going to make the shape draggable
$shape.draggable({
drag: function () { //when the user drag the shape, we going to
send to the server the x, y values
hub.server.moveShape(this.offsetLeft, this.offsetTop || 0);
});
MoveShapeHub.cs
using
Microsoft.AspNet.SignalR;
Microsoft.AspNet.SignalR.Hubs;
System;
System.Collections.Generic;
System.Linq;
System.Threading.Tasks;
System.Web;
namespace
SignalR.MoveShape
{
public
static
class
UserHandler
//this static class is to store the number of
users conected at the same time
HashSet<
string
> ConnectedIds =
new
>();
[HubName(
"moveShape"
)]
//this is for use a name to use in the client
MoveShapeHub : Hub
void
moveShape(
int
x,
y)
// this method will be called from
the client, when the user drag/move the shape
Clients.Others.shapeMoved(x, y);
// this method will send the coord x, y
to the other users but the user draging the shape
override
Task OnConnected()
//override OnConnect, OnReconnected and OnDisconnected
to know
if
a user
is
connected or disconnected
UserHandler.ConnectedIds.Add(Context.ConnectionId);
//add a connection id to the list
Clients.All.usersConnected(UserHandler.ConnectedIds.Count());
//this will send to ALL the clients
the number of users connected
return
base
.OnConnected();
Task OnReconnected()
Task OnDisconnected()
UserHandler.ConnectedIds.Remove(Context.ConnectionId);
.OnDisconnected();
Globa.asax.cs
System.Web.Routing;
System.Web.Security;
System.Web.SessionState;
SignalR
Global : System.Web.HttpApplication
Application_Start()
// Register the default hubs route: ~/signalr
RouteTable.Routes.MapHubs();
protected
Session_Start(
object
sender, EventArgs e)
Application_BeginRequest(
Application_AuthenticateRequest(
Application_Error(
Session_End(
Application_End(
Richard Mueller edited Original. Comment: Replace RGB values with standard color names in HTML to restore colors