问题描述
我很新的SignalR,我试图将其与AngularJS网站,整合的WebAPI。我跟着马丽娟的例子开始。但是,正如我所说,我工作的网站将生活在不同的服务器到的WebAPI项目上。
I very new to SignalR and I am trying to integrate it with an AngularJS site and WebAPI. I followed greate this example to begin with. However, as I mentioned, the website I am working on will live on a different server to the WebAPI project.
在我的开发环境我已经建立了一个折角本地站点,在IIS托管在localhost:60000 / 127.0.0.1:60000和的WebAPI住在localhost:31374(在VS2013,使用IIS防爆preSS。 )这是模拟2个不同的服务器的项目将居住。
In my dev environment I have set up a local site for Angular, hosted in IIS on localhost:60000/127.0.0.1:60000 and the WebAPI lives on localhost:31374 (in VS2013, using IIS Express.) This is to simulate the 2 different servers the projects will live on.
在我的角度项目中我试图做连接到SignalR枢纽以下内容:
In my Angular project I am trying to connect to the SignalR hub by doing the following:
var connection = $.hubConnection();
$.connection.hub.url = "http://localhost:31374/signalr";
//$.connection.url = "/signalr";
//$.conenction.baseUrl = "http://localhost:31374";
this.proxy = connection.createHubProxy('foos');
console.clear();
// start connection
console.log(connection);
connection.start();
this.proxy.on('newFoo', function (data) {
$rootScope.$emit("newFoo", data);
});
在控制台中的结果是这样的:
The result in the console looks like this:
在我的code,你可以看到,我想设置枢纽URL(和评论仅低于它,我已经试过手动设置连接对象的属性。)然而,在截图中可以看到,无论是网址或BASEURL就是我将其设置为,其实BASEURL仍然指向回角网站上的,在的http://本地主机:31374 / 。
In my code you can see in that I am trying to set the hub url (and in the comments just below it, that I have tried to set the connection object's properties manually.) However, in the screenshot you can see that neither the URL or the baseURL is what I set it to be, in fact the baseURL still points back to the Angular site on http://127.0.0.1:60000/, in stead of http://localhost:31374/.
我是什么失踪?
推荐答案
此设置看起来有点过。具体来说,也许你的 connection.start()
通话是不正确的。我在角与以下设置的正常工作做到了这一点与跨域服务器
This set up looks a little off. Specifically, perhaps your connection.start()
call is not correct. I've done this in Angular with a cross domain server with the following setup which works fine
var proxy;
$(function () {
$.connection.hub.url = 'http://localhost:31374/signalr';
$.connection.hub.start({ xdomain: true })
.done(function () {
console.log('Connected. connectionId : ' + $.connection.hub.id);
})
.fail(function () {
console.log('Could not connect!');
});
proxy = $.connection.yourServerHubName;
proxy.client.yourClientHubMethod = function () {
// your client hub functions
};
});
此外,一定要检查您正在加载正确生成的js文件在你的HTML
Also be sure to check you are loading the proper generated .js file in your HTML
<script src="//localhost:31374/signalr/hubs"></script>
这篇关于没有得到分散/不同的角度现场交谈SignalR中的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!