asp.net core signalR (1)


服务端SignalR支持

// 加入signalR,需要cors支持
builder.Services.AddSignalR();
string[] urls = { "http://localhost:3000" };
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(builder => builder.WithOrigins(urls)
    .AllowAnyMethod().AllowAnyHeader().AllowCredentials());
});

app.UseCors();
app.MapHub("/hubs/chatroomhub");
    public class MyChatHub : Hub
    {
        public Task SendPublicMessage(string message)
        {
            string connId = Context.ConnectionId;
            string msg = $"{connId} {DateTime.Now}:{message}";

            return Clients.All.SendAsync("PublicMessageReceived", msg);
        }
    }

client端SignalR支持

. 使用yarn管理包,Vue作为client端框架

yarn create @vitejs/app chatroom
npm install @microsoft/signalr

yarn
yarn dev