错误:无法启动连接:错误:WebSocket 连接失败。在服务器上找不到连接,端点可能不是 SignalR 端点,服务器上不存在连接 ID,或者存在阻止 WebSocket 的代理。如果您有多个服务器,请检查粘性会话是否已启用。
WebSocketTransport.js:49 WebSocket 连接到“ws://xxxxxx/product/web-services/hubs/spreadhub”失败:
Angular.ts
ngOnInit(): void {
// For signalR Hub connection
this.connection = new HubConnectionBuilder()
.withUrl('http://xxx.xxx.com/production/web-services/hubs/spreadhub', { // localhost from **AspNetCore3.1 service**
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
})
.build();
this.connection.on('dataReceived', (data: string) => {
var model = JSON.parse(data);
});
this.connection.start().then(() => { // to start the server
console.log('server connected!!!');
}).catch(err => console.log(err));
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的SingulaR示例,我已经添加到传统的ASP.Net MVC应用程序中.
以下是各个部分:
OWIN启动课程
[assembly: OwinStartup(typeof (Startup))]
namespace MyApp.Web
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Run Code Online (Sandbox Code Playgroud)
SignalR Hub
using System.Collections.Generic;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace MyApp.Web
{
[HubName("podService")]
public class PodServiceHub : Hub
{
public PodServiceHub()
{
;
}
public IEnumerable<string> GetMessages()
{
return new[] {"blah", "blah", "blah"};
}
}
}
Run Code Online (Sandbox Code Playgroud)
服务器端外观
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace MyApp.Web
{
public class PodService
{
PodService(IHubConnectionContext<dynamic> clients)
{
Clients = clients;
}
public PodService()
: …Run Code Online (Sandbox Code Playgroud)