我编写了一个signalR套接字,但是它不起作用。
ConfigureServices
services.AddSignalR();
配置
app.UseSignalR(builder =>
{
builder.MapHub<Socket>("/Socket");
});
套接字类
public class Socket :Hub
{
public async Task SendMessage( string message)
{
Console.WriteLine(message);
await Clients.All.SendAsync( "newMessage", "anonymous", message);
}
}
客户代码
<script src="https://localhost:44353/lib/signalr/signalr.js"></script>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
const connection = new signalR.HubConnectionBuilder()
.withUrl("/Socket")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.start().then(function () {
console.log("connected");
});
</script>
当我在浏览器上运行https://localhost:44353/Socket这个URL时,得到此结果“需要连接ID”,这是什么问题?有人可以帮我吗?