分布式部署


  WebSocket 可以采用多台服务器集群部署,以提高服务的呑吐率。在 Fireasy 中分布式部署很简单(前提是需要配置分布式缓存管理器),你只需要向 IOC 注册 DistributedClientManager 类,并指定 AppKey 即可。如下所示:

namespace demo
{
    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.AddFireasy(Configuration).AddIoc();
            services.AddTransient<IClientManager, DistributedClientManager>();
        }
        
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseWebSockets(options =>
            {
                //心跳检测时间间隔
                options.HeartbeatInterval = TimeSpan.FromSeconds(10);
                options.ReceiveBufferSize = 4 * 1024;
                options.KeepAliveInterval = TimeSpan.FromSeconds(10);
                //这个必须指定且几台服务器均一致
                options.AppKey = "demo_ws";
                //添加处理器
                options.MapHandler<NotifyHandler>("/wsNotify");
                options.MapHandler<ChatHandler>("/wsChat");
            });
        }
    }
}