Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created November 12, 2012 02:14
Show Gist options
  • Save davidfowl/4057152 to your computer and use it in GitHub Desktop.
Save davidfowl/4057152 to your computer and use it in GitHub Desktop.
Blog Post
public class Chat : Hub
{
public void Send(string message)
{
// Call send on everyone
Clients.All.send(message);
// Call send on everyone except the caller
Clients.Others.send(message);
// Call send on everyone except the specified connection ids
Clients.AllExcept(Context.ConnectionId).send(message);
// Call send on the caller
Clients.Caller.send(message);
// Call send on everyone in group "foo"
Clients.Group("foo").send(message);
// Call send on everyone else but the caller in group "foo"
Clients.OthersInGroup("foo").send(message);
// Call send on everyone in "foo" excluding the specified connection ids
Clients.Group("foo", Context.ConnectionId).send(message);
// Call send on to a specific connection
Clients.Client(Context.ConnectionId).send(message);
}
}
[Authorize]
public class Chat : Hub
{
}
[Authorize(Mode = AuthorizeMode.Outgoing)]
public class Chat2 : Hub
{
[Authorize]
public void Send(string message)
{
}
public void Send2(string message)
{
}
}
public class Chat : Hub
{
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected()
{
return base.OnDisconnected();
}
public override Task OnReconnected()
{
return base.OnReconnected();
}
}
<script type="text/javascript">
$(function () {
var chat = $.connection.chat;
chat.client.send = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
$.connection.hub.connectionSlow(function() {
alert('There seems to be some connectivity issues...');
});
$.connection.hub.start();
});
</script>
<script src="Scripts/jquery.signalR-1.0.0-alpha2.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.client.send = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
$("#broadcast").click(function () {
// Call the chat method on the server
chat.server.send($('#msg').val());
});
// Start the connection
$.connection.hub.start();
});
</script>
<div>
<input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />
<ul id="messages">
</ul>
</div>
namespace MySignalRApplication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
GlobalHost.HubPipeline.AddModule(new LoggingPipelineModule());
}
}
public class LoggingPipelineModule : HubPipelineModule
{
protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context)
{
Debug.WriteLine("=> Invoking " + context.MethodDescriptor.Name + " on hub " + context.MethodDescriptor.Hub.Name);
return base.OnBeforeIncoming(context);
}
protected override bool OnBeforeOutgoing(IHubOutgoingInvokerContext context)
{
Debug.WriteLine("<= Invoking " + context.Invocation.Method + " on client hub " + context.Invocation.Hub);
return base.OnBeforeOutgoing(context);
}
}
}
namespace MySignalRApplication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
GlobalHost.HubPipeline.EnableAutoRejoiningGroups();
}
}
}
[assembly: PreApplicationStartMethod(typeof(MySignalRApplication.RegisterHubs), "Start")]
namespace MySignalRApplication
{
public static class RegisterHubs
{
public static void Start()
{
// Register the default hubs route: ~/signalr/hubs
RouteTable.Routes.MapHubs();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment