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

限制消息发送次数

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

 

减少消息数据的大小

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

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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中需要加入一些代码将这些可读性差的属性,转换成可读性好的属性

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
 
};
posted @   LamondLu  阅读(445)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩