コード例 #1
0
 /// <summary>
 /// 启用 ImServer 服务端
 /// </summary>
 /// <param name="app"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseImServer(this IApplicationBuilder app, ImServerOptions options)
 {
     app.Map(options.PathMatch, appcur =>
     {
         var imserv = new ImServer(options);
         if (isUseWebSockets == false)
         {
             try
             {
                 var webSocketOptions = new WebSocketOptions()
                 {
                     KeepAliveInterval = TimeSpan.FromSeconds(10),  //向客户端发送“ping”帧的频率,以确保代理保持连接处于打开状态
                 };
                 isUseWebSockets = true;
                 appcur.UseWebSockets(webSocketOptions); // appcur.UseWebSockets(webSocketOptions);
             }
             catch (Exception ex)
             {
                 Log.Error($"初始化WebSocker错误:{ex.Message}");
             }
         }
         appcur.Use((ctx, next) =>
                    imserv.Acceptor(ctx, next));
     });
     return(app);
 }
コード例 #2
0
 /// <summary>
 /// 启用 ImServer 服务端
 /// </summary>
 /// <param name="app"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseImServer(this IApplicationBuilder app, ImServerOptions options)
 {
     app.Map(options.PathMatch, appcur =>
     {
         var imserv = new ImServer(options);
         if (isUseWebSockets == false)
         {
             try
             {
                 var webSocketOptions = new WebSocketOptions()
                 {
                     KeepAliveInterval = TimeSpan.FromSeconds(10), //向客户端发送“ping”帧的频率,以确保代理保持连接处于打开状态
                     ReceiveBufferSize = 4 * 1024                  //用于接收数据的缓冲区的大小。 只有高级用户才需要对其进行更改,以便根据数据大小调整性能。
                 };
                 isUseWebSockets = true;
                 appcur.UseWebSockets(); // appcur.UseWebSockets(webSocketOptions);
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"UseWebSockets:{ex.Message}");
             }
         }
         appcur.Use((ctx, next) =>
                    imserv.Acceptor(ctx, next));
     });
     return(app);
 }
コード例 #3
0
 /// <summary>
 /// 启用 ImServer 服务端
 /// </summary>
 /// <param name="app"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseImServer(this IApplicationBuilder app, ImServerOptions options)
 {
     app.Map(options.PathMatch, appcur =>
     {
         var imserv = new ImServer(options);
         if (isUseWebSockets == false)
         {
             isUseWebSockets = true;
             appcur.UseWebSockets();
         }
         appcur.Use((ctx, next) =>
                    imserv.Acceptor(ctx, next));
     });
     return(app);
 }
コード例 #4
0
ファイル: ImServer.cs プロジェクト: yangli1128/im
 /// <summary>
 /// 启用 ImServer 服务端
 /// </summary>
 /// <param name="app"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IApplicationBuilder UseImServer(this IApplicationBuilder app, ImServerOptions options)
 {
     app.Map(options.PathMatch, appcur =>
     {
         var imserv = new ImServer(options);
         if (isUseWebSockets == false)
         {
             isUseWebSockets = true;
             appcur.UseWebSockets(new WebSocketOptions()
             {
                 KeepAliveInterval = TimeSpan.FromSeconds(10)
             });
         }
         appcur.Use((ctx, next) =>
                    imserv.Acceptor(ctx, next));
     });
     return(app);
 }
コード例 #5
0
 public ImServer(ImServerOptions options) : base(options)
 {
     _server = options.Server;
     _redis.Subscribe(($"{_redisPrefix}Server{_server}", RedisSubScribleMessage));
 }
コード例 #6
0
ファイル: ImServer.cs プロジェクト: yangli1128/im
    public ImServer(ImServerOptions options) : base(options)
    {
        _server = options.Server;
        //_redis.Subscribe(($"{_redisPrefix}Server{_server}", RedisSubScribleMessage));
        _redis.Subscribe(($"{_redisPrefix}Server_ImCore", RedisSubScribleMessage));
        //移除在线
        var c = GetClientListByOnline();

        foreach (var v in c)
        {
            _redis.HDel($"{_redisPrefix}Online", v.ToString());
            //_redis.Eval($"if redis.call('HINCRBY', KEYS[1], '{v}', '-1') <= 0 then redis.call('HDEL', KEYS[1], '{v}') end return 1",
            //$"{_redisPrefix}Online");
        }
        new Thread(_ =>
        {
            while (true)
            {
                try
                {
                    List <WebSocket> list = new List <WebSocket>();
                    foreach (var v in _clients)
                    {
                        foreach (var s in v.Value)
                        {
                            try
                            {
                                var outgoing = new ArraySegment <byte>(Encoding.UTF8.GetBytes(""));
                                CancellationTokenSource source = new CancellationTokenSource(1000);
                                s.Value.socket.SendAsync(outgoing, WebSocketMessageType.Text, true, source.Token).Wait();
                                if (source.IsCancellationRequested)
                                {
                                    Console.WriteLine("超时发送");
                                    list.Add(s.Value.socket);
                                }
                            }
                            catch {
                                list.Add(s.Value.socket);
                            }
                            if (s.Value.socket.State != WebSocketState.Open)
                            {
                                list.Add(s.Value.socket);
                            }
                        }
                    }
                    foreach (var v in list)
                    {
                        Console.WriteLine("remove websocket");
                        try
                        {
                            v.Abort();
                        }
                        catch {
                        }
                    }
                }
                catch { }
                Thread.Sleep(100);
            }
        });
    }
コード例 #7
0
 public ImServer(ImServerOptions options) : base(options)
 {
     _server = options.Server;
 }