SignalR学习笔记(四) 性能优化

it2024-07-31  65

限制消息发送次数

这种方式在学习笔记(二)-  高并发应用中介绍过,在客户端和服务器端使用定时器来减少消息发送的次数

 

减少消息数据的大小

服务器端,可以使用JsonIgnore, 来忽略不需要序列化的属性,并使用JsonProperty给需要序列化的属性起一个简短的名字

 

 

using Newtonsoft.Json; using System; public class ShapeModel {       [JsonProperty("l")]     public double Left { get; set; }       [JsonProperty("t")]     public double Top { get; set; }     // We don't want the client to get the "LastUpdatedBy" property       [JsonIgnore]     public string LastUpdatedBy { get; set; } }

 

 

但是这样随之而来的问题就是JavaScript中读取这些属性的可读性变差了,前台开发人员很可能不知道l字段是什么,t字段是什么。

 

所以Javascript中需要加入一些代码将这些可读性差的属性,转换成可读性好的属性

 

 

function reMap(smallObject, contract) {     var largeObject = {};     for (var smallProperty in contract) {         largeObject[contract[smallProperty]] = smallObject[smallProperty];     }     return largeObject; } var shapeModelContract = {     l: "left",     t: "top" }; myHub.client.setShape = function (shapeModelSmall) {     var shapeModel = reMap(shapeModelSmall, shapeModelContract);     // shapeModelSmall has "l" and "t" properties  but after remapping     // shapeModel now has "left" and "top" properties };

转载于:https://www.cnblogs.com/lwqlun/p/9095092.html

相关资源:微软SignalR案例解读和测试
最新回复(0)