Socket.D v2.5.12

应用 - 流控 + 告警传递

</> markdown

告警协议(Session::sendAlarm),传递到对端时会变为异常

1、客户端

async def main():
    #::打开客户端会话
    clientSession = await (SocketD.create_client("sd:ws://127.0.0.1:8602/?u=a&p=2")
                            .config(lambda c : c.fragment_size(1024 * 1024))
                            .open())

    # 发送并请求
    await (clientSession.send_and_subscribe("/demo",  EntityDefault().range(5, 5).meta_put("videoId", "42E056E1-B4B7-4EF4-AC4B-AEE77EDB0B86"))
     .then_reply(lambda r: r.dataSize()>0 and log.info(str(r)))
     .then_error(lambda err : log.info(str(err))))

2、服务端

async def doOn_demo(s:Session, m:Message):
    if  m.is_subscribe() == False:
        await s.send_alarm(m, "此事件只支持订阅模式")
        return

    videoId   = m.meta_as_long("videoId")
    rangeStart = m.range_start()
    rangeSize  = m.range_size()

    if videoId == 0 or rangeSize == 0:
        await s.send_alarm(m, "参数不合规")
        return

    for i in range(rangeStart, rangeSize):
        await s.reply(m, StringEntity("i-"+str(i)))
        
    await s.reply_end(m, StringEntity(""))


async def main():
    #::启动服务端
    await (SocketD.create_server("sd:ws")
           .config(lambda c: c.port(8602))
           .listen(EventListener().do_on("/demo", doOn_demo))
           .start())