Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            #region Swagger;NetCore3.0以上版本写法一样
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");
                c.RoutePrefix = "";//路径配置,设置为空,表示直接访问该文件。
            });
            #endregion

            #region 使用websocket(扩展方法)
            //app.AddWebSocketService<WSTestWSBehavior>("/test");//单个链接

            //多个链接,服务端发送:WebAPIWebSocketService.CurrentServiceHost("/test").Sessions.SendTo(dataStr, key);
            var test = Configuration.GetSection("WebSocketHosts:Server:TestUrl").Value;
            WebAPIWebSocketService.AddWebSocketService <WSTestWSBehavior>(test);
            app.AddWebSocketMap();
            #endregion

            #region Consul注册
            //站点启动完成--执行且只执行一次
            //this.Configuration.ConsulRegist();
            #endregion
        }
Exemplo n.º 2
0
        /// <summary>
        /// 服务端发送消息
        /// </summary>
        /// <param name="dataStr"></param>
        public static void Send(string dataStr)
        {
            var po = new ParallelOptions
            {
                MaxDegreeOfParallelism = 10
            };

            if (WebAPIWebSocketService.ServiceHosts.Count > 0)
            {
                List <string> errorSessionIds = new List <string>();

                if (WSTestWSBehavior.GroupSessions.TryGetValue(sessionKey, out ConcurrentDictionary <string, WSTestWSBehavior> dic))
                {
                    Parallel.ForEach(dic.Keys, po, key =>
                    {
                        try
                        {
                            WebAPIWebSocketService.CurrentServiceHost(testPath).Sessions.SendTo(dataStr, key);
                        }
                        catch (Exception)
                        {
                            errorSessionIds.Add(key);
                        }
                    });
                }
                if (errorSessionIds != null && errorSessionIds.Count > 0)
                {
                    foreach (string errorSessionId in errorSessionIds)
                    {
                        WebSocketBehavior webSocketBehavior = WebAPIWebSocketService.CurrentServiceHost(testPath).Sessions.Remove(errorSessionId);
                        if (webSocketBehavior != null)
                        {
                            webSocketBehavior.QuitFromGroups();
                            webSocketBehavior.Dispose();
                        }
                    }
                }
            }
        }