最近做一个和SmartHome相关的项目,文档不全不说,连个像样的Demo都没,痛苦!!当然,这是题外话。今天来说说项目中主要用到的通讯协议:json-rpc,简单地说,它是以json格式进行的远程调用,是一种比xml-rpc更lightweight的协议,具体的介绍可参考json-rpc官网Wiki。这里参考了Jayrock: JSON and JSON-RPC for .NET 也使用Jayrock来说说json-rpc的应用。

1.准备工作

首先,添加引用,先通过Nuget获取Json.net,然后引用Jayrock.dll和Jayrock.Json.dll

建立一些Model,这个根据实际情况来创建。我这里服务器用户登录需要的json的格式,例如:

{"method":"Login","params":{"username":"test2014","password":"test2014"}}

我们这里创建这么几个实体:

#region Login
    public class LoginModel
    {
        public string method { get; set; }
        public LoginParams _params { get; set; }
    }
    public class LoginParams
    {
        public string username { get; set; }
        public string password { get; set; }
    }
    public class LoginReturnModel
    {
        public string result { get; set; }
        public string info { get; set; }
    } 
    #endregion

    #region GetDeviceInfo
    public class GetDeviceInfoModel
    {
        public string method { get; set; }
        public GetDeviceInfoParams _params { get; set; }
    }
    public class GetDeviceInfoParams
    {

    }
    public class GetDeviceInfoReturnModel
    {
        public string result { get; set; }
        public GetDiveInfoReturnInfo[] info { get; set; }
    }
    public class GetDiveInfoReturnInfo
    {
        public string deviceid { get; set; }
        public string deviceno { get; set; }
        public string identify { get; set; }
        public string lable { get; set; }
    } 
    #endregion
View Code

相关文章:

  • 2022-12-23
  • 2021-09-24
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
猜你喜欢
  • 2021-11-19
  • 2021-05-12
  • 2022-12-23
  • 2021-05-04
相关资源
相似解决方案