Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created October 29, 2011 11:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidfowl/1324342 to your computer and use it in GitHub Desktop.
Save davidfowl/1324342 to your computer and use it in GitHub Desktop.
C# Hubs API
using System;
using System.Threading;
using System.Threading.Tasks;
using SignalR.Hubs;
namespace Server
{
public class MyHub : Hub
{
public void Foo()
{
Caller.bar("Hello");
Caller.baz(1);
Caller.foo(new { Name = "David Fowler", Age = 24 });
Caller.multipleParams(1, 2, new { Name = "John" });
Caller.notify();
}
public string GetString()
{
return "David";
}
public Task<int> GetTask(Person person)
{
return Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
return person.Age;
});
}
public void ThrowError()
{
throw new InvalidOperationException("Throwing an exception");
}
public string InitilizeState()
{
Caller.name = "Damian";
return Caller.id;
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
}
using System;
using SignalR.Client.Hubs;
namespace Sample
{
class Program
{
static void Main()
{
var connection = new HubConnection("http://localhost:42079/");
var myHub = connection.CreateProxy("Server.MyHub");
// Static type
myHub.On<Person>("foo", person =>
{
Console.WriteLine("static => Name: {0}, Age:{1}", person.Name, person.Age);
});
// No params, just notify me
myHub.On("notify", () => Console.WriteLine("Notified!"));
// Dynamic type (no need to specify type arguments if you like dynamic :))
myHub.On("foo", person =>
{
Console.WriteLine("dynamic => Name: {0}, Age: {1}", person.Name, person.Age);
});
// Simple types
myHub.On<string>("bar", s =>
{
Console.WriteLine("Bar({0})", s);
});
myHub.On<int>("baz", i =>
{
Console.WriteLine("Baz({0})", i);
});
myHub.On<int, int, dynamic>("multipleParams", (a, b, c) =>
{
Console.WriteLine("multipleParams({0}, {1}, {2})", a, b, c);
});
// IObservable<T>
myHub.AsObservable("foo")
.Subscribe(args =>
{
Console.WriteLine(args[0]);
});
connection.Start().Wait();
// Invoke the foo method
// await myHub.Invoke("Foo");
myHub.Invoke("Foo").Wait();
// Invoke hub method with a result
// string result = await myHub.Invoke("GetString");
myHub.Invoke<string>("GetString")
.ContinueWith(t =>
{
Console.WriteLine("GetString => " + t.Result);
});
// Method that takes a complex parameter
var p = new Person { Name = "John Doe", Age = 24 };
myHub.Invoke<int>("GetTask", p)
.ContinueWith(t =>
{
Console.WriteLine("Age: {0}", t.Result);
});
myHub.Invoke("ThrowError")
.ContinueWith(t =>
{
Console.WriteLine("Faulted: {0} -> {1}", t.IsFaulted, t.Exception.GetBaseException().Message);
});
// Client side state
myHub["id"] = "some value set from client";
myHub.Invoke<string>("InitilizeState")
.ContinueWith(t =>
{
Console.WriteLine("Name set from server => {0}", myHub["name"]);
Console.WriteLine("Result: {0}", t.Result);
});
Console.Read();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment