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?
OK! David's comments got me going in the right direction. Also noticed this was updated, didnt notice this before. Scroll to the bottom for the cross domain support
1.) Updated to version .5
2.) Modified my connection information in javascript to
$.connection.hub.url = 'http://localhost:8081/test/signalr';
$.connection.hub.start({ transport: 'longPolling', xdomain: true }).done(function () {
//alert("staring hub connection: " + $.connection.hub.id);
setCookie("srconnectionid", $.connection.hub.id);
});
3.) I had to change the HubConnection
var connection = new HubConnection("http://localhost:8081/test/");
4.) I had to change the IHubProxy
IHubProxy myHub = connection.CreateProxy("UserAccountHub");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With