繁体   English   中英

无法从Javascript连接到SignalR集线器

[英]Failing to connect to a SignalR hub from Javascript

我正在尝试连接到与MVC项目位于单独的Web Api项目中的SignalR集线器,但连接始终失败并显示错误。 我正在本地测试,但显然如果您使用IE,它将为您处理跨域。

在Chrome中

http:// localhost:53453 / signalr / negotiate?clientProtocol = 1.5&connectionData =%5B%7B%22name%22%3A%22gweventhub%22%7D%5D&_ = 1430342757730无法加载资源:服务器响应状态为404 (未找到)

在IE中

连接错误:协商请求期间出错。

这是集线器的服务器代码。

[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);
    }
}

这是尝试连接到集线器的Javascript。 它位于单独的MVC应用程序中。

$(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);
        });
});

在连接过程中我缺少什么吗?

尝试将您的代码修改为以下内容

 $.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); }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM