无法连接到从Javascript一个SignalR枢纽 [英] Failing to connect to a SignalR hub from Javascript
问题描述
我试图连接到位于从MVC项目一个单独的Web API项目,但连接保持与错误而失败一个SignalR枢纽。我在本地测试,但显然,如果您使用IE浏览器会处理跨域你。
在Chrome浏览器
<一个href=\"http://localhost:53453/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22gweventhub%22%7D%5D&_=1430342757730\" rel=\"nofollow\">http://localhost:53453/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22gweventhub%22%7D%5D&_=1430342757730
无法加载资源:服务器回应的404状态
(未找到)
块引用>在IE
连接错误:协商请求时出错
块引用>下面是服务器code为中心。
[HubName(GWEventHub)]
公共类GatewayEventHub:集线器
{
公众覆盖任务OnConnected()
{
this.Groups.Add(this.Context.ConnectionId,this.Context.Request.User.Identity.Name);
返回(base.OnConnected());
} 公众覆盖任务OnDisconnected(布尔stopCalled)
{
this.Groups.Remove(this.Context.ConnectionId,this.Context.Request.User.Identity.Name);
返回base.OnDisconnected(stopCalled);
}
}下面是JavaScript尝试连接到轮毂上。这是位于一个单独的MVC应用程序。
$(函数(){
VAR连接= $ .hubConnection(的http://本地主机:53453');
connection.logging = TRUE; 变种毂= connection.createHubProxy('GWEventHub');
hub.logging = TRUE; 功能的handleMessage(消息){
的console.log('从轮毂的消息:'+消息);
} hub.on('sendUserMessage'的handleMessage); connection.start()
.done(函数(){
警报('连接到GatewayEventHub,连接ID ='+ connection.id);
})
.fail(函数(五){
的console.log(连接错误+ E);
});
});有我丢失的东西在连接过程?
解决方案通过修改code以下
尝试
\r\r\r\r$ connection.hub.url =HTTP://本地主机:53453 / signalr;\r
VAR枢纽= $ .connection.GWEventHub;\r
\r
//你的日志记录等功能的其余部分放在这里\r
\r
$ .connection.hub.start()。完成(功能(){\r
警报('连接到GatewayEventHub,连接ID ='+ $ .connection.hub.id);\r
})\r
.fail(函数(五){\r
的console.log(连接错误+ E);\r
});\r
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?
解决方案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); });
这篇关于无法连接到从Javascript一个SignalR枢纽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!