Skip to content

Instantly share code, notes, and snippets.

@kiliman
Forked from davidfowl/AllMethods.cs
Created November 12, 2012 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiliman/4060169 to your computer and use it in GitHub Desktop.
Save kiliman/4060169 to your computer and use it in GitHub Desktop.
Blog Post
public class Chat : Hub
{
public void Send(string message)
{
// Call addMessage on everyone
Clients.All.addMessage(message);
// Call addMessage on everyone except the caller
Clients.Others.addMessage(message);
// Call addMessage on everyone except the specified connection ids
Clients.AllExcept(Context.ConnectionId).addMessage(message);
// Call addMessage on the caller
Clients.Caller.addMessage(message);
// Call addMessage on everyone in group "foo"
Clients.Group("foo").addMessage(message);
// Call addMessage on everyone else but the caller in group "foo"
Clients.OthersInGroup("foo").addMessage(message);
// Call addMessage on everyone in "foo" excluding the specified connection ids
Clients.Group("foo", Context.ConnectionId).addMessage(message);
// Call addMessage on to a specific connection
Clients.Client(Context.ConnectionId).addMessage(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.addMessage = 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.addMessage = 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();
}
}
}
@kiliman
Copy link
Author

kiliman commented Nov 12, 2012

Changed calls to .send to .addMessage since that is what the client method is in the Javascript example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment