Using Messagepack in ASP. NET Core SignalR

Source: Internet
Author: User

It ' s like JSON.
But fast and small.

Messagepack is an efficient binary serialization format. It lets your Exchange data among multiple languages like JSON. But it ' s faster and smaller. Small integers is encoded into a single byte, and typical short strings require only one extra byte in addition to the St Rings themselves.

Messagepack is an efficient binary serialization format. It allows you to exchange data between languages like JSON. But it's faster and smaller than JSON. A small integer is encoded into a byte, and the short string is just a byte larger than its length.

The use of Messagepack in SignalR is also simple:

 Public voidconfigureservices (iservicecollection services) {//Custom ConfigurationServices. Configure<dbsetting> (Configuration.getsection ("ConnectionStrings")); //https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1Services. configure<cookiepolicyoptions> (options ={options. checkconsentneeded= Context =true; Options. Minimumsamesitepolicy=Samesitemode.none;            }); //Add Hangfire serviceServices. Addhangfire (config =CONFIG.            Useredisstorage (Redis)); //Services. Addhangfire (config = config.            Usesqlserverstorage ("<connection string>")); //Add Cookie middlewareServices. Addauthentication (sharedoptions ={sharedoptions.defaultscheme=Cookieauthenticationdefaults.authenticationscheme; //sharedoptions.defaultchallengescheme = openidconnectdefaults.authenticationscheme;            })            //. ADDAZUREADB2C (opts = Configuration.bind ("AZUREADB2C", opts)). Addcookie (opts ={opts. Loginpath=NewPathString ("/account/login"); OPTs. Accessdeniedpath=NewPathString ("/account/denied");            }); Services. ADDSIGNALR (Options=            {                //Faster pings for testingOptions. KeepAliveInterval = Timespan.fromseconds (5); })            . Addmessagepackprotocol (Options={options. Formatterresolvers=NewList<messagepack.iformatterresolver>() {MessagePack.Resolvers.StandardResolver.Instance}; })            . Addredis (Configuration.getconnectionstring ("Redis")); //return capitalization issuesServices. Addmvc (). Addjsonoptions (Option= option. Serializersettings.contractresolver =NewNewtonsoft.Json.Serialization.DefaultContractResolver ()); }

. NET Client

var New Hubconnectionbuilder ()                        . Withurl ("/chathub")                        . Addmessagepackprotocol ()                        . Build ();

JavaScript Client

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>

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/

Using Messagepack in ASP. NET Core SignalR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.