Created
January 10, 2013 06:10
-
-
Save DamianEdwards/4499825 to your computer and use it in GitHub Desktop.
SignalR peer-to-peer using class-less hubs idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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