SignalR 客户端连接到不同的服务器
SignalR client connect to different server
我在 ASP.Net MVC 应用程序中使用 SignalR 版本 2.x,并且在我的 angular 客户端应用程序中使用相同版本的 Signalr。
Asp.net MVC 应用程序托管在 http://localhost:42080
中,angular 应用程序托管在 http://localhost:4200
中。
我已经安装 Microsoft.AspNet.SignalR
并在 mvc 应用程序中启用了 cors。
[HubName("msg")]
public class MessageHub : Hub
{
public void Send(string user, string message)
{
Clients.User(user).Send(user, message);
}
}
我想从我的 angular 应用程序连接到信号器服务器,但不能。
const connection = $.hubConnection(this.config.AdminUrl); // http://localhost:42080
const chat = connection.createHubProxy('msg'); // chat.server or chat.client are undefined
我也试过:
$.connection.hub.url = 'http://localhost:42080/signalr';
var hub = $.connection.msg; // hub = undefined
$.connection.hub.start() // this will result Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/js'></script>
如何连接到托管在不同服务器上的信号服务器?
您需要像这样配置您的 Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
}
}
备注
Don't set jQuery.support.cors to true in your code.
Don't set jQuery.support.cors to true
SignalR handles the use of CORS. Setting jQuery.support.cors to true
disables JSONP because it causes SignalR to assume the browser
supports CORS.
如果要连接到不同的服务器,请在调用 start 方法之前指定 URL,如下例所示:
JavaScript
$.connection.hub.url = '<yourbackendurl>;
备注
Normally you register event handlers before calling the start method
to establish the connection.
详情可见here
我在 ASP.Net MVC 应用程序中使用 SignalR 版本 2.x,并且在我的 angular 客户端应用程序中使用相同版本的 Signalr。
Asp.net MVC 应用程序托管在 http://localhost:42080
中,angular 应用程序托管在 http://localhost:4200
中。
我已经安装 Microsoft.AspNet.SignalR
并在 mvc 应用程序中启用了 cors。
[HubName("msg")]
public class MessageHub : Hub
{
public void Send(string user, string message)
{
Clients.User(user).Send(user, message);
}
}
我想从我的 angular 应用程序连接到信号器服务器,但不能。
const connection = $.hubConnection(this.config.AdminUrl); // http://localhost:42080
const chat = connection.createHubProxy('msg'); // chat.server or chat.client are undefined
我也试过:
$.connection.hub.url = 'http://localhost:42080/signalr';
var hub = $.connection.msg; // hub = undefined
$.connection.hub.start() // this will result Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/js'></script>
如何连接到托管在不同服务器上的信号服务器?
您需要像这样配置您的 Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
}
}
备注
Don't set jQuery.support.cors to true in your code.
Don't set jQuery.support.cors to true
SignalR handles the use of CORS. Setting jQuery.support.cors to true disables JSONP because it causes SignalR to assume the browser supports CORS.
如果要连接到不同的服务器,请在调用 start 方法之前指定 URL,如下例所示:
JavaScript
$.connection.hub.url = '<yourbackendurl>;
备注
Normally you register event handlers before calling the start method to establish the connection.
详情可见here