Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created September 8, 2014 16:33
Show Gist options
  • Save ChrisRisner/c35e342a4bcd7a1fdc01 to your computer and use it in GitHub Desktop.
Save ChrisRisner/c35e342a4bcd7a1fdc01 to your computer and use it in GitHub Desktop.
private async Task AddHub(string address)
{
Trace.TraceInformation("AddHub: " + address);
// create a connection to a hub
var hubConnection = new HubConnection(address);
hubConnection.Headers.Add("ORLEANS", "GRAIN");
var hub = hubConnection.CreateHubProxy("playerHub");
await hubConnection.Start();
hubs.Add(address, new Tuple<HubConnection, IHubProxy>(hubConnection, hub));
}
$(document).ready(function () {
var hub = $.connection.playerHub;
hub.client.playerUpdate = function (message) {
alert(message);
}
$.connection.hub.start().done(function () {
alert('listening for messages');
console.log("listening for messages");
})
});
public class PlayerHub : Hub
{
public void PlayerUpdate(string message)
{
Trace.TraceInformation("PlayerHub.PlayerUpdate");
// Forward a single messages to all browsers
Clients.Group("BROWSERS").playerUpdate(message);
}
public override System.Threading.Tasks.Task OnConnected()
{
Trace.TraceInformation("PlayerHub.OnConnected");
if (Context.Headers.Get("ORLEANS") != "GRAIN")
{
// This connection does not have the GRAIN header, so it must be a browser
// Therefore add this connection to the browser group
Groups.Add(Context.ConnectionId, "BROWSERS");
}
return base.OnConnected();
}
}
PlayerHub hub = new PlayerHub();
hub.PlayerUpdate("BLAH!");
return "Hub should be tested";
{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":" at Microsoft.AspNet.SignalR.Hubs.SignalProxy.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)\r\n at CallSite.Target(Closure , CallSite , Object , String )\r\n
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)\r\n
at AdventureTerreWebRole.Controllers.GameOneController.<Get>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment