ASP.NET Core SignalR 中使用 MessagePack 序列化
2018-06-18 14:53
1071 查看
It's like JSON.
but fast and small.MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.
MessagePack 是一个高效的二进制序列化格式。它让你像JSON一样可以在各种语言之间交换数据。但是它比JSON更快、更小。小的整数会被编码成一个字节,短的字符串仅仅只需要比它的长度多一字节的大小。
在 SignalR 中使用 MessagePack 也很简单:
public void ConfigureServices(IServiceCollection services) { //自定义配置 services.Configure<DbSetting>(Configuration.GetSection("ConnectionStrings")); //https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1 services.Configure<CookiePolicyOptions>(options => { options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); //添加 Hangfire 服务 services.AddHangfire(config => config.UseRedisStorage(Redis)); // services.AddHangfire(config => config.UseSqlServerStorage("<connection string>")); //添加 Cookie 中间件 services.AddAuthentication(sharedOptions => { sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; // sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; }) //.AddAzureAdB2C(opts => Configuration.Bind("AzureAdB2C", opts)) .AddCookie(opts => { opts.LoginPath = new PathString("/account/login"); opts.AccessDeniedPath = new PathString("/account/denied"); }); services.AddSignalR(options => { //Faster pings for testing options.KeepAliveInterval = TimeSpan.FromSeconds(5); }) .AddMessagePackProtocol(options => { options.FormatterResolvers = new List<MessagePack.IFormatterResolver>() { MessagePack.Resolvers.StandardResolver.Instance }; }) .AddRedis(Configuration.GetConnectionString("Redis")); //返回大小写问题 services.AddMvc() .AddJsonOptions(option => option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver()); }
.NET 客户端
var hubConnection = new HubConnectionBuilder() .WithUrl("/chatHub") .AddMessagePackProtocol() .Build();
JavaScript 客户端
npm install @aspnet/signalr-protocol-msgpack
<script src="~/lib/signalr/signalr.js"></script> <script src="~/lib/msgpack5/msgpack5.js"></script> <script src="~/lib/signalr/signalr-protocol-msgpack.js"></script>
const connection = new signalR.HubConnectionBuilder() .withUrl("/hqHub") .withHubProtocol(new signalR.protocols.msgpack.MessagePackHubProtocol()) .build();
REFER:
https://docs.microsoft.com/en-us/aspnet/core/signalr/messagepackhubprotocol?view=aspnetcore-2.1
https://damienbod.com/2018/03/19/using-message-pack-with-asp-net-core-signalr/https://socket.io/docs/
相关文章推荐
- 在ASP.NET CORE 2.0使用SignalR技术
- Asp.Net Core中使用Newtonsoft.Json进行序列化处理解决返回值首字母小写
- 在 ASP.NET Core 中使用 SignalR
- 在ASP.NET Core下使用SignalR技术
- asp.net core视图中使用Json序列化模型
- 在ASP.NET Core下使用SignalR技术
- 详解在ASP.NET Core下使用SignalR技术
- 在ASP.NET CORE 2.0使用SignalR技术
- ASP.NET Core的实时库: SignalR简介及使用
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- ASP.NET C#使用JavaScriptSerializer实现序列化与反序列化得到JSON
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 关于ASP.NET SignalR的Group使用
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 关于ASP.NET SignalR的Group使用
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能 [转]
- 使用SignalR在Asp.net中实现进度条
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能