我有简单的推送通知SignalR应用程序,并且必须将其托管在Azure上,它在我的本地主机上运行良好,但在天蓝色下则无法运行。下面是代码。这是微服务解决方案,因此某些配置与微服务有关。
public class Startup
{
private const string SwaggerApiVersion = "v1";
private const string SwaggerApiTitle = "Messaging Microservice API";
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.AddMvc();
services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
services.AddScoped<IAuthenticationRepository, AuthenticationRepository>();
Utility.Swagger.SwaggerConfig.ConfigureService(services, SwaggerApiVersion, SwaggerApiTitle,
Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Tracking.SetConfiguration();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseAuthentication();
app.UseSignalR(routes =>
{
routes.MapHub<NotificationHub.NotificationHub>("/notificationHub");
});
app.UseMvc();
Utility.Swagger.SwaggerConfig.ConfigureApplication(app, SwaggerApiVersion, SwaggerApiTitle);
Tracking.TrackEvent(Assembly.GetEntryAssembly().GetName().Name + ".Started");
}
}
下面是集线器类。
public class NotificationHub : Hub
{
public async Task SendMessage(List<string> userIds)
{
await Clients.All.SendAsync("ReceiveMessage", userIds);
}
public async Task SendToAll(string name, string message)
{
await Clients.All.SendAsync("SendToAll", name, message);
}
}
实际上没有从我的react客户端建立连接,并且我无法调用集线器方法(SendMessage,SendToAll)。
下面是响应客户端连接代码。
componentDidMount = () => {
let url = Config['MessagingMicroservice'].replace(/\bapi\b(?!.*?\bapi\b)/, 'notificationHub');
const hubConnection = new HubConnectionBuilder().withUrl(url).build();
this.setState({ hubConnection }, () => {
this.state.hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('));
this.state.hubConnection.on('ReceiveMessage', (userIds) => {
const usrIds = `${userIds}`;
if (usrIds.includes(this.state.userId)) {
this.getNotificationCount(this.state.userId);
}
});
});
};
我正在摆脱错误。
WebSocket connection to 'wss://isambard-dev.capitawfm.co.uk:19081/IsambardServiceFabric/MessagingMicroservice/notificationHub/?id=OcLN9AYHOVp8Hr4t3mPVPA'
failed: Error during WebSocket handshake: Unexpected response code: 504
https://isambard-dev.capitawfm.co.uk:19081/IsambardServiceFabric/MessagingMicroservice/notificationHub/?id=jcEzEb0coVTp51hQeFlzOw
Error: Failed to start the transport 'ServerSentEvents': Error: Error occurred