public class TestHub : Hub { //給 client 呼叫 server 上的方法 public void LaSai(string message) { //server 丟一些 result 回去給 client Clients.All.hello(message); } }
加入類別 Startup
1 2 3 4 5 6 7 8
public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here app.MapSignalR(); } }
Error: Failed to load assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' referenced by the server. If your application uses a newer version of this assembly, this error is likely due to a missing or inaccurate binding redirect. If your server application has a 'Web.config' or '[AppName].exe.config' file with binding redirects in it, provide the path to that file with the '/configFile' parameter. If you have already provided a '/configFile' parameter, make sure the config file it points to has a binding redirect for 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' within it. See https://aka.ms/about-binding-redirects for more information on binding redirects
Access to XMLHttpRequest at 'http://localhost:23007/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22testhub%22%7D%5D&_=1639937665706' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here //app.MapSignalR(); app.Map("/signalr", map => { map.UseCors(CorsOptions.AllowAll); var hubConfiguration = new HubConfiguration { }; map.RunSignalR(hubConfiguration); }); } }