Skip to content

Instantly share code, notes, and snippets.

@bradygaster-zz
Created August 22, 2012 15:06
Show Gist options
  • Save bradygaster-zz/3426554 to your computer and use it in GitHub Desktop.
Save bradygaster-zz/3426554 to your computer and use it in GitHub Desktop.
SignalR 0.5.3 Sample Code
[HubName("chat")]
public class ChatHub : Hub
{
public void SendMessage(dynamic message)
{
Clients.receiveMessage(message);
}
}
private static void Connect()
{
var connection = new HubConnection("http://localhost:58416/");
var chat = connection.CreateProxy("chat");
chat.On<string>("addMessage", Console.WriteLine);
try
{
connection.Start().Wait();
string msg = null;
while ((msg = Console.ReadLine()) != null)
{
chat.Invoke("send", msg).Wait();
}
}
catch (Exception ex)
{
using (var error = ex.GetError()) // NEW ERROR HANDLING FEATURES
{
Console.WriteLine(error.StatusCode);
}
}
}
var chat = connection.createProxy('chat');
var connection = $.hubConnection('http://mysite.azurewebsites.net');
public void DynamicInvoke(string method)
{
IClientProxy proxy = Caller;
IClientProxy clientsProxy = Clients;
IClientProxy groupProxy = Clients["foo"];
proxy.Invoke(method);
}
connection.logging = true;
var connection = $.hubConnection();
connection.start(function () {
$('#submitButton').removeAttr('disabled');
});
$(function () {
var connection = $.hubConnection();
connection.start(function () {
$('#submitButton').removeAttr('disabled');
});
var chat = connection.createProxy('chat');
$('#submitButton').click(function () {
var msg = {
username: $('#username').val(),
message: $('#message').val()
};
chat.invoke('SendMessage', msg);
});
});
chat.on('receiveMessage', function (m) {
$('#messages').append('<li><b>' + m.username + '</b>:' + m.message + '</li>');
});
public void SendMessage(dynamic message)
{
var user = message.username;
if (String.IsNullOrEmpty(message.username.ToString()))
{
throw new ApplicationException("No username provided!");
}
Clients.receiveMessage(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment