Getting the Client Address in SignalR

2023年8月4日 48点热度 0人点赞 0条评论
内容目录

In SignalR, to obtain the client's connection information, you can use IHttpConnectionFeature to retrieve the client's communication IP and port.

            // Get the service
            var feature = Context.Features.Get<IHttpConnectionFeature>();
            var httpContext = Context.GetHttpContext();
			
            ArgumentNullException.ThrowIfNull(feature);
            ArgumentNullException.ThrowIfNull(httpContext);

            // Header query information
            var clientIdSV = httpContext.Request.Headers["ClientId"];
            var clientId = clientIdSV.FirstOrDefault();
            ArgumentNullException.ThrowIfNull(clientId);

            // Get the client's communication address
            var remoteAddress = feature.RemoteIpAddress;
            ArgumentNullException.ThrowIfNull(remoteAddress);
            var remotePort = feature.RemotePort;

If you want to get the client's Hub ConnectionId, you can use

base.Context.ConnectionId

痴者工良

高级程序员劝退师

文章评论