Socket.D v2.5.12

了解三个基础发送接口

</> markdown

1、发送(像 websocket)

send

//::打开客户端会话
const clientSession = await SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
        .open();

//发送
clientSession.send("/demo", SocketD.newEntity("hello wrold!"));

2、发送并请求 + 一个答复(像 http)

sendAndRequest + replyEnd(或者 reply。反正只收一个答复,管它是不是最后的)

//::打开客户端会话
const clientSession = await SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
        .open();

//发送并请求(且,等待答复)
let reply = clientSession.sendAndRequest("/demo", SocketD.newEntity("hello wrold!")).await();

sendAndRequest 的同步模式(一般需要指定超时,默认为 10s)

clientSession.sendAndRequest("/demo", SocketD.newEntity("hello wrold!")).await();

sendAndRequest 的异步模式(一般需要指定超时,默认为 10s)

//示例,指定10秒超时
clientSession.sendAndRequest("/demo", SocketD.newEntity("hello wrold!"), 10_000).thenReply(reply=>{
    //收到答复
}).thenError(error=>{
   //如果有异常(比如超时,或者协议告警)
});

3、发送并订阅 + 多个答复(像 reactive stream)

sendAndSubscribe + reply(*n) + replyEnd

//::打开客户端会话
ClientSession clientSession = SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
        .open();

//发送并订阅(且,接收答复流)
clientSession.sendAndSubscribe("/demo", SocketD.newEntity("hello wrold!")).thenReply(reply=>{
    //如果需要识别是否为最后一个答复?
    if(reply.isEnd()){

    }
}).thenError(error=>{
   //如果有异常(比如超时,或者协议告警)
});

sendAndSubscribe 只有异步模式(如果有需要,可以指定具体超时。默认为 2h)

//示例,指定60秒超时
clientSession.sendAndSubscribe("/demo", SocketD.newEntity("hello wrold!"), 60_000).thenReply(reply=>{
    //如果需要识别是否为最后一个答复?
    if(reply.isEnd()){

    }
}).thenError(error=>{
   //如果有异常(比如超时,或者协议告警)
});