forked from SignalR/SignalR
-
Notifications
You must be signed in to change notification settings - Fork 0
QuickStart Persistent Connections
davidfowl edited this page Nov 6, 2011
·
18 revisions
This quickstart was designed to get you up and running quickly with a working SignalR sample using a PersistentConnection. For more details, See the documentation.
Go to NuGet and install the SignalR package into a WebApplication:
Install-Package SignalR
Create a class the derives from PersistentConnection:
using System.Threading.Tasks;
using SignalR;
public class MyConnection : PersistentConnection {
protected override Task OnReceivedAsync(string clientId, string data) {
// Broadcast data to all clients
return Connection.Broadcast(data);
}
}
Make a route for your connection:
Global.asax
using System;
using System.Web.Routing;
using SignalR.Routing;
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
}
}
<script src="http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var connection = $.connection('echo');
connection.received(function (data) {
$('#messages').append('<li>' + data + '</li>');
});
connection.start();
$("#broadcast").click(function () {
connection.send($('#msg').val());
});
});
</script>
<input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />
<ul id="messages">
</ul>