2

I'm trying to connect to a SignalR hub that is located in a seperate Web Api project from a MVC project but the connection keeps failing with error. I'm testing locally but apparently if you use IE it will handle Cross domain for you.

In Chrome

http://localhost:53453/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22gweventhub%22%7D%5D&_=1430342757730 Failed to load resource: the server responded with a status of 404 (Not Found)

In IE

Connection Error: Error during negotiation request.

Here is the server code for the hub.

[HubName("GWEventHub")]
public class GatewayEventHub : Hub
{
    public override Task OnConnected()
    {
        this.Groups.Add(this.Context.ConnectionId, this.Context.Request.User.Identity.Name);
        return (base.OnConnected());
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        this.Groups.Remove(this.Context.ConnectionId, this.Context.Request.User.Identity.Name);
        return base.OnDisconnected(stopCalled);
    }
}

Here is the Javascript attempting to connect to the hub. This is located in a separate MVC app.

$(function () {
    var connection = $.hubConnection('http://localhost:53453');
    connection.logging = true;

    var hub = connection.createHubProxy('GWEventHub');
    hub.logging = true;

    function handleMessage(message) {
        console.log('message from hub: ' + message);
    }

    hub.on('sendUserMessage', handleMessage);

    connection.start()
        .done(function() {
            alert('connect to GatewayEventHub, Connection ID = ' + connection.id);
        })
        .fail(function(e) {
            console.log('Connection Error ' + e);
        });
});

Is there something I'm missing in the connection process?

1 Answer 1

1

Try by modifying your code to below

$.connection.hub.url = "http://localhost:53453/signalr";
var hub = $.connection.GWEventHub;

//rest of your logging and other functions goes here
 
$.connection.hub.start().done(function() {
        alert('connect to GatewayEventHub, Connection ID = ' + $.connection.hub.id);
    })
    .fail(function(e) {
        console.log('Connection Error ' + e);
    });

Sign up to request clarification or add additional context in comments.

10 Comments

so that seems to have gotten me a little closer but now the error is saying "Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/js'></script>." I tried adding <script src='/signalr/js'></script> to the view that is calling the signalr code but still no luck.
Could you try below? <script src="~/signalr/hubs"></script> Add tilde (~) in front of it.
unfortunately even after adding that I'm still getting the same error :(
if I run debug from Visual Studio a break occurs saying "JavaScript critical error at line 1, column 1 in localhost:63596/signalr/hubs\n\nSCRIPT1002: Syntax error"
It seems like your hub is not generating properly. Are you bundling that dynamic hub on BundleConfig? If so, please remove it from bundling and try again. Also try by directly calling the server hub like localhost:53453/signalr/hubs and see what's happening.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.