1 Star 0 Fork 2

Argo / TouchSocketBlazor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

图片名称

雄关漫道真如铁,而今迈步从头越。从头越,苍山如海,残阳如血。

中文 | English

🎀描述

【开源版】

名称 地址 描述
NuGet version (TouchSocket) Gitee
Github
TouchSocket这是一个轻量级的、支持插件的综合网络通信库。
基础通信功能包含Tcp、Udp、Ssl、Rpc、Http等。其中http
服务器支持WebSocket、静态网页、XmlRpc、WebApi、JsonRpc
等扩展插件。和自定义协议的TouchRpc,支持Ssl加密、异步调用、
权限管理、错误状态返回、服务回调、分布式调用等。在空载函数
执行时,10万次调用仅3.8秒,在不返回状态时,仅0.9秒。
NuGet version (TouchSocket.AspNetCore) Gitee
Github
TouchSocket.AspNetCore是适用于AspNetCore的专属版本。

【企业版】

名称 地址 描述
NuGet version (TouchSocketPro) Gitee
Github
TouchSocketPro是TouchSocket的企业版,这在原有基础之上,
还有一些企业版功能,详情请看企业版相关
NuGet version (TouchSocketPro.AspNetCore) Gitee
Github
TouchSocketPro.AspNetCore是适用于AspNetCore的专属版本。

🖥支持环境

  • .NET Framework4.5及以上。
  • .NET Core3.1及以上。
  • .NET Standard2.0及以上。

🥪支持框架

  • Console
  • WPF
  • Winform
  • Blazor Server
  • Xamarin
  • MAUI
  • Avalonia
  • Mono
  • Unity 3D
  • 其他(即所有C#系)

🌴TouchSocket特点速览

传统IOCP和TouchSocket的IOCP模式

TouchSocket的IOCP和传统也不一样,就以微软官方示例为例,他是使用MemoryBuffer开辟一块内存,均分,然后给每个会话分配一个区接收,等收到数据后,再复制接收的数据,然后把复制的数据进行处理。而TouchSocket是每次接收之前,从内存池拿一个可用内存块,然后直接用于接收,等收到数据以后,直接就把这个内存块抛出处理,这样就避免了复制操作,虽然只是细小的设计,但是在传输10w64kb的数据时,性能相差了10倍

数据处理适配器

相信大家都使用过其他的Socket产品,那么TouchSocket在设计时也是借鉴了其他产品的优秀设计理念,数据处理适配器就是其中之一,但和其他产品的设计不同的是,TouchSocket的适配器功能更加强大,易用,且灵活。它不仅可以提前解析数据包,还可以解析数据对象,可以随时替换,然后立即生效。例如:可以使用固定包头对数据进行预处理,从而解决数据分包粘包的问题。也可以直接解析HTTP数据协议、WebSocket数据协议等。

兼容性与适配

TouchSocket提供多种框架模型,能够完全兼容基于TCP、UDP协议的所有协议。例如:TcpService与TcpClient,其基础功能和Socket一模一样,只是增强了框架的坚固性并发性,将连接接收数据通过事件的形式抛出,让使用者能够更加友好的使用。

🔗联系作者

🌟说明文档

👑功能导图

图片名称

✨简单示例

以下仅以最简方式创建示例,更多详情请查看说明文档

【TcpService】

TcpService service = new TcpService();
service.Connecting += (client, e) => { };//有客户端正在连接
service.Connected += (client, e) => { };//有客户端连接
service.Disconnected += (client, e) => { };//有客户端断开连接
service.Received += (client, byteBlock, requestInfo) =>
{
    //从客户端收到信息
    string mes = byteBlock.ToString();
    Console.WriteLine($"已从{client.ID}接收到信息:{mes}");

    client.Send(mes);//将收到的信息直接返回给发送方

    //client.Send("id",mes);//将收到的信息返回给特定ID的客户端

    var clients = service.GetClients();
    foreach (var targetClient in clients)//将收到的信息返回给在线的所有客户端。
    {
        if (targetClient.ID != client.ID)
        {
            targetClient.Send(mes);
        }
    }
};

service.Setup(new TouchSocketConfig()//载入配置     
    .SetListenIPHosts(new IPHost[] { new IPHost("127.0.0.1:7789"), new IPHost(7790) })//同时监听两个地址
    .SetMaxCount(10000)
    .SetThreadCount(100))
    .Start();//启动

【TcpClient】

TcpClient tcpClient = new TcpClient();
tcpClient.Connected += (client, e) => { };//成功连接到服务器
tcpClient.Disconnected += (client, e) => { };//从服务器断开连接,当连接不成功时不会触发。
tcpClient.Received += (client, byteBlock, requestInfo) =>
{
    //从服务器收到信息
    string mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);
    Console.WriteLine($"接收到信息:{mes}");
};

//声明配置
TouchSocketConfig config = new TouchSocketConfig();
config.SetRemoteIPHost(new IPHost("127.0.0.1:7789"))
    .UsePlugin()
    .SetBufferLength(1024 * 10);

//载入配置
tcpClient.Setup(config);
tcpClient.Connect();
tcpClient.Send("RRQM");

【TcpClient 断线重连】 在Config的插件配置中,首先得启用插件,然后使用重连插件即可。

.UsePlugin()
.ConfigurePlugins(a=> 
{
   a.UseReconnection(5, true, 1000);
});

【FixedHeaderPackageAdapter包模式】

该适配器主要解决TCP粘分包问题,数据格式采用简单而高效的“包头+数据体”的模式,其中包头支持:

  • Byte模式(1+n),一次性最大接收255字节的数据。
  • Ushort模式(2+n),一次最大接收65535字节。
  • Int模式(4+n),一次最大接收2G数据。

以上数据头均采用TouchSocketBitConverter的默认端模式(小端模式),使用者可以根据需求切换默认端模式。

TouchSocketBitConverter.DefaultEndianType = EndianType.Little;

【CustomFixedHeaderDataHandlingAdapter】

用户自定义固定包头适配器,主要帮助用户解决具有固定包头的数据帧信息。例如:下列数据格式,仅需要实现几个接口,就能完成解析,详细操作请参照API。

|1|1|1|**********|

【CustomUnfixedHeaderDataHandlingAdapter】

用户自定义不固定包头适配器,主要帮助用户解决具有包头不固定的数据帧信息。例如:最典型的HTTP数据包,其数据头和数据体由“\r\n”隔开,而数据头又因为请求者的请求信息的不同,头部数据也不固定,而数据体的长度,也是由数据头的ContentLength的值显式指定的,所以可以考虑使用CustomUnfixedHeaderDataHandlingAdapter解析,也是仅通过简单的开发,就能实现。

【Http服务器】

var service = new HttpService();

service.AddPlugin<MyHttpPlug>();
service.AddPlugin<HttpStaticPagePlugin>().
   AddFolder("../../../../../api");//添加静态页面

service.AddPlugin<WebSocketServerPlugin>().//添加WebSocket功能
   SetWSUrl("/ws");

service.AddPlugin<MyWebSocketPlugin>();//添加WS事务触发。

service.AddPlugin<MyWSCommandLinePlugin>();//添加WS命令行事务。

var config = new RRQMConfig();
config.UsePlugin()
    .SetReceiveType(ReceiveType.Auto)
    .SetListenIPHosts(new IPHost[] { new IPHost(7789) });

service.Setup(config).Start();
Console.WriteLine("Http服务器已启动");
Console.WriteLine("浏览器访问:http://127.0.0.1:7789/index.html");
Console.WriteLine("WS访问:ws://127.0.0.1:7789");

🧲应用场景模拟

场景入口


致谢

谢谢大家对TouchSocket的支持,如果还有其他问题,请提交Issue,或者加群QQ:234762506讨论。

支持作者

支持入口

GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2022 Copyright (C) 2022 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

这是基于TouchSocket的Blazor管理端。 展开 收起
C#
LGPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/Longbow/touchsocketblazor.git
git@gitee.com:Longbow/touchsocketblazor.git
Longbow
touchsocketblazor
TouchSocketBlazor
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891