Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created January 10, 2013 06:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamianEdwards/4499825 to your computer and use it in GitHub Desktop.
Save DamianEdwards/4499825 to your computer and use it in GitHub Desktop.
SignalR peer-to-peer using class-less hubs idea
<div>
<input id="msg" type="text" />
<input id="send" type="button" value="Send" />
</div>
<ul id="msgs">
</ul>
<script src="Scripts/jquery-1.8.3.js"></script>
<script src="Scripts/jquery.signalR-1.5.0.js"></script>
<script src="/signalr/hubs"></script>
<script>
var conn = $.hubConnection(),
chat = conn.createProxy("chat");
chat.client.newMessage = function (msg) {
$("#msgs").append("<li>" + msg + "</li>");
};
conn.start().done(function() {
$("#send").click(function() {
// The "all" member will be populated with methods based on those added
// above to the "client" member (e.g. newMessage) as well as any methods
// defined in a server hub class with the same name, if one exists (optional).
chat.all.newMessage($("#msg").val());
});
});
</script>
// This hub is optional, it's not required for the client to work.
// The client's call to "all.newMessage" will result in this Hub's
// NewMessage method being called so it can persist the message.
public class Chat : Hub
{
public void NewMessage(string message)
{
MyDataLayer.SaveMessage(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment