信号器出错。在运行的应用程序上显示错误“找不到方法”。 连接有问题。
我正在使用Signalr进行聊天。该项目位于Reactjs和asp.net核心中。 react app是通过creat-react-app(控制台)创建的。
// reactjs *****中的代码
import React, { Component } from 'react';
import { HubConnection } from 'signalr-client-react';
class SingnalR extends Component {
constructor(props) {
super(props);
this.state = {
bookingMessage: '',
bookingHubConnection: null
};
}
componentDidMount() {
const bookingHubConnection = new HubConnection('http://localhost:5000/chatHub')
this.setState({ bookingHubConnection }, () => {
this.state.bookingHubConnection.start()
.then(() => console.log('Signalr started '))
.catch((err) => console.log('Error connecting signalr - ' + err));
this.state.bookingHubConnection.on('booking', (message) => {
const bookingMessage = message;
this.setState({ bookingMessage });
});
});
}
render() {
return (
<div>
<div>message from server {this.state.bookingMessage}</div>
userName <input id="userName" />
userMessage<input id="userMessage" />
<button id = "sendMessage"> sendMessage</button>
</div>
)
}
}
export default SingnalR;
// asp.net核心的代码*******
//代码chatHub
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace TinyHouseApi.SignalR
{
public class ChatHub:Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMesage", user, message);
}
}
}
//code of Startupfile
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("testDB")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddAutoMapper();
//configure SignalR
services.AddSignalR();
// configure strongly typed settings objects
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthentication();
app.UseMvc();
//SignalR
app.UseSignalR(routes =>
{
routes.MapHub<ChatHub>("/chatHub");
});
}
}
答案 0 :(得分:0)
您使用 signalr-client-react 巫婆取决于 ms-signalr-client 巫婆取决于 jquery.signalR.js 巫婆是 signalR 客户端的旧版本,并且与 ASP.Net Core SignalR 不兼容。
要使用最新版本的 SignalR 和 ASP.Net Core ,您应该使用@aspnet/signalr软件包。您不能使用 signalr-client-react