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

限制消息发送次数

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

 

减少消息数据的大小

服务器端,可以使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值