Basically I have 3 apps. A web app, Nservicebus, and a signalr self hosting server for my hubs. I am currently using signalr version .4. Basically the web app initiates a connection with the self hosted server on page load. The User does an action which sends a command down the service bus. The service bus then connects to the signalr server to notify all the web clients.
My Web page:
<script src="http://localhost:8081/test/signalr/hubs" type="text/javascript"></script>
<script>
jQuery.support.cors = true;
var myHub;
$.connection.hub.url = 'http://localhost:8081/test/signalr/hubs';
$(function () {
myHub = $.connection.userAccountHub;
myHub.ChangeNameUpdated = function (connId) {
alert("recieved signalr message");
};
$.connection.hub.start().done(function() {
var myClientId = $.connection.hub.id;
setCookie("srconnectionid", myClientId);
});
});
</script>
My SignalR server:
static void Main(string[] args)
{
Debug.Listeners.Add(new ConsoleTraceListener());
Debug.AutoFlush = true;
string url = "http://localhost:8081/test/";
var server = new Server(url);
server.EnableHubs();
server.Start();
Console.WriteLine("Server running on {0}", url);
while (true)
strong text{
ConsoleKeyInfo ki = Console.ReadKey(true);
if (ki.Key == ConsoleKey.X)
{
break;
}
}
}
My Hub inside my Signalr server project
public class UserAccountHub : Hub
{
public void UsersNameChanged(string passedThroughConnectionId)
{
Clients.ChangeNameUpdated(passedThroughConnectionId);
}
}
My Call from my nservicebus
//connect to signalRServer
var connection = new HubConnection("http://localhost:8081/test/signalr/hubs");
IHubProxy myHub = connection.CreateProxy("SignalRServer.Hubs.UserAccountHub");
//Credentials are inherited from the application
connection.Credentials = CredentialCache.DefaultNetworkCredentials;
var test = message.GetHeader("signalrConnectionId");
connection.Start().Wait();
myHub.Invoke("UsersNameChanged", message.GetHeader("signalrConnectionId")).ContinueWith(task =>
{
//for debuging purposes
if (task.IsFaulted)
{
Console.WriteLine(
"An error occurred during the method call {0}",
task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Successfully called MethodOnServer");
}
});
I open up 2 browsers. I initiate the process on the second browser, and only the first one receives notifications. The second browser doesnt.
I don't see any issues that stand out when I run fiddler either.
Is there a better approach to this implementation, or am I missing something?
var connection = new HubConnection("http://localhost:8081/test/signalr/hubs");
The above line is incorrect. You Create a connection to the hub url like this:var connection = new HubConnection("http://localhost:8081/test/");
Same for your javascript hub url, it should be: $.connection.hub.url = 'localhost:8081/test/signalr'; – davidfowl