Kurento的JsonRpc文档 6.6.1-第5章 JSON-RPC javascript客户端 翻译

这是用javascript语言实现的客户端连接kurento-jsonrpc-server服务,或者连接其它websocket服务实现了json-rpc协议的服务器。它允许javascipt程序通过json-rpc调用kurento-jsonrpc-server,它也发布一个bower依赖项(https://github.com/Kurento/kurento-jsonrpc-bower):

5.1jsonrpc客户端

5.1.1创建客户端

创建的客户端发送请求,你必须创建一个配置对象像下面的例子:

var configuration = {

      hearbeat: 5000,

      sendCloseMessage : false,

      ws : {

        uri : ws_uri,

        useSockJS: false,

        onconnected : connectCallback,

        ondisconnect : disconnectCallback,

        onreconnecting : disconnectCallback,

        onreconnected : connectCallback

      },

      rpc : {

        requestTimeout : 15000,

        treeStopped : treeStopped,

        iceCandidate : remoteOnIceCandidate,

      }

    };

 

var jsonRpcClientWs = new JsonRpcClient(configuration);

配置对象有几个选项:一方面配置传输,另一方面配置客户端接收到响应回调方法,也可以配置连接关闭前的心跳间隔。

配置

{

    heartbeat: interval in ms for each heartbeat message,

    sendCloseMessage: true / false, before closing the connection, it sends a close_session message,

    ws: {

        uri: URItoconntectto,

        useSockJS: true(useSockJS)/false(useWebSocket)bydefault,

        onconnected: callback method to invoke when connection is successful,

        ondisconnect: callback method to invoke when the connection is lost,

        onreconnecting: callback method to invoke when the client is reconnecting,

        onreconnected: callback method to invoke when the client succesfully reconnects

    },

    rpc: {

        requestTimeout: timeoutforarequest,

        sessionStatusChanged: callback method for changes in session status,

        mediaRenegotiation: mediaRenegotiation

        ...

    [Other methods you can add on rpc field are:

treeStopped : treeStopped

   iceCandidate : remoteOnIceCandidate]

    }

}

如果定义了心跳,间隔几毫秒客户端发送ping消息到服务从而保持连接。

5.2发送请求

Json-rpc调用代表着使用发送方法发送请求对象到服务器,请求对象有以下成员:

method:调用方法的名称字符串

params:调用方法的参数值,这个成员可以省略,它是一个json对像定义于服务器。

callback:当服务器产生错误或者响应回调这个方法

var params = {

             interval: 5000

              };

 

jsonrpcClient.send(“ping”, params , function(error, response){

         if(error) {

            ...

         } else {

            ...

         }

      });

5.2.1服务响应

当服务接收rpc调用,它将应答一个响应,除了通知以外,响应由单个的JSON对象,对象包括以下成员:

jsonrpc:json-rpc的协议版本字符串,本例使用”2.0”

result:这个成员只有成功才存在,值由调用服务器确定。

error:这个成员只有存在错误信息才存在,这个类型是错误对象

id:这是请求成员,这个必须与请求的id成员一致。

响应的包括”result”或者”error”成员,但不是全部存在。

5.2.2 error对象

当rpc调用遇到错误时,响应对象包括error成员,error的值是一个对象,对象成员包括:

code:整型的错误类型

message:错误的简短描述

data:包含有关错误的其他信息的原始值或结构化值,可能是省略由服务器定义(例如,详细的错误信息,嵌套的错误等)

5.2.3其它方法

close:客户端关闭jsonRpcClient

reconnect:尝试重新连接

forceClose:只于测试,强制关闭连接

5.3 websocket重连

Jsonrpc客户端使用websocket进行重连,允许连接一直是活动的。

当发生事件时调用下面的回调方法:

• onConnected

• onDisconnected

• onReconnecting

• onReconnected

下面的配置是jsonrpc客户端配置对象的一部分

{

   uri: URItoconntectto,

   useSockJS: true(useSockJS)/false(useWebSocket)bydefault,

   onconnected: callback method to invoke when connection is successful,

   ondisconnect: callback method to invoke when the connection is lost,

   onreconnecting: callback method to invoke when the client is reconnecting,

   onreconnected: callback method to invoke when the client succesfully reconnects

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值