Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(plugin): WebSocket和Tcp的plugin同一个ip下高并发下数据会session的data会转移到别的session下,导致请求错误 #48

Open
1 task done
Leoc82 opened this issue Dec 14, 2024 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@Leoc82
Copy link

Leoc82 commented Dec 14, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

bug(): WebSocketReadPlugin和Tcp的plugin,session是单例共享模式。同一个ip下高并发下发送的数据会从 A session的data转移到B session,导致数据无法返回正确的客户端。我没用hosting.没法用依赖注入的方式

public async Task Start()
		{
			if (Service == null) Service = new HttpService();
			await Service.SetupAsync(new TouchSocketConfig()//加载配置
				 .SetListenIPHosts(7901)
				 .ConfigureContainer(a =>
				 {
					 a.AddConsoleLogger();
				 })
				 .ConfigurePlugins(a =>
				 {
					 a.UseWebSocket()//添加WebSocket功能
					   .SetWSUrl("")//设置url直接可以连接。
					   .UseAutoPong();//当收到ping报文时自动回应pong

					 a.Add<LimitWebSocketConnectionsPlugin>();
					 a.Add<>();
				 }));
			await Service.StartAsync();
		}

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

.net core 8.0

Anything else?

No response

@Leoc82 Leoc82 added the bug Something isn't working label Dec 14, 2024
@RRQM RRQM changed the title bug(): WebSocket和Tcp的plugin同一个ip下高并发下数据会session的data会转移到别的session下,导致请求错误 bug(plugin): WebSocket和Tcp的plugin同一个ip下高并发下数据会session的data会转移到别的session下,导致请求错误 Dec 14, 2024
@RRQM RRQM added question Further information is requested and removed bug Something isn't working labels Dec 14, 2024
@RRQM
Copy link
Owner

RRQM commented Dec 14, 2024

首先,这不属于一个bug,而是设计。

插件的默认工作模式就是非注入单例。所以当你什么都不做时,整个HttpService都将使用一个插件实例。

所以我们才建议使用IServiceCollection来实现Scoped注入。因为默认的IOC容器是不支持Scoped注入的。

一般情况下,强烈建议使用Host通用主机模型构建。你可以参考:

https://touchsocket.net/docs/current/generichost
https://touchsocket.net/blog/CreatehighlyavailableTcpService

如果你还是想要通过默认创建,那请按照下列步骤:

首先,请先使用nuget安装:TouchSocket.Core.DependencyInjection

然后在声明插件时,使用PluginOption特性,并设置FromIoc 为true。

[PluginOption( FromIoc = true)]
internal class LimitWebSocketConnectionsPlugin:PluginBase
{

}

然后其余逻辑如下:

//创建服务容器
var IocServices = new ServiceCollection();

//添加自定义插件
IocServices.AddScoped<LimitWebSocketConnectionsPlugin>();

var Service = new HttpService();
await Service.SetupAsync(new TouchSocketConfig()//加载配置
     .SetListenIPHosts(7901)
     .UseAspNetCoreContainer(IocServices)//使用Ioc容器
     .ConfigureContainer(a =>
     {
         a.AddConsoleLogger();
     })
     .ConfigurePlugins(a =>
     {
         a.UseWebSocket()//添加WebSocket功能
           .SetWSUrl("")//设置url直接可以连接。
           .UseAutoPong();//当收到ping报文时自动回应pong

         //使用自定义插件
         a.Add<LimitWebSocketConnectionsPlugin>();
     }));
await Service.StartAsync();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants