我第一次使用SignalR,并尝试从Angular连接到集线器。我正在尝试在此LINK
中实现相同的功能我不确定此协商来自何处
OPTIONS http://localhost:5001/messageHub/negotiate net :: ERR_CONNECTION_REFUSED
这是我的代码。
var scriptNode = document.createElement("script");
scriptNode.src = "/wp-content/themes/gowebsites/gw-addon/js/wow.js";
var cssNode = document.createElement("link");
cssNode.href = "/wp-content/themes/gowebsites/gw-addon/css/animations.css";
cssNode.rel = "stylesheet";
head.appendChild(scriptNode);
head.appendChild(cssNode);
这是appsettings.Development.json
public startConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl('http://localhost:5001/messageHub')
.build();
this.hubConnection
.start()
.then(() => alert('Connection started'))
.catch(err => alert('Error while starting SignalR connection: ' + err));
}
答案 0 :(得分:0)
要在asp.net核心中运行signalR,您必须像这样配置这些步骤
以您的打字稿代码
this._hubConnection = new HubConnectionBuilder()
.withUrl("/signalr")
.configureLogging(LogLevel.Error)
.build();
this._hubConnection.start().catch(err => console.error(err.toString()));
Startup.cs
services.AddSignalR();
app.UseSignalR(routes =>
{
routes.MapHub<ConnectionHub>("/connectionHub");
});
和您的中心班
public class ConnectionHub : Hub
{
public async Task Send(string userId)
{
var message = $"Send message to you with user id {userId}";
await Clients.Client(userId).SendAsync("ReceiveMessage", message);
}
public string GetConnectionId()
{
return Context.ConnectionId;
}
}