Socket.D v2.5.11

Helloworld

</> markdown

其它内容参考 Java 开发(接口与包都一样)


//打开客户端会话,并监听(用 url 形式打开)
const session = await SocketD.createClient("sd:ws://127.0.0.1:8602/?token=1b0VsGusEkddgr3d")
        .listen(SocketD.newEventListener()
            .doOnOpen(s => { //会话打开时
                //...
            }).doOnMessage((s, m) => { //收到任意消息时
                //打印
                System.out.println(m);
            }).doOn("/demo", (s, m) => { //收到"/demo"事件的消息时
                if (m.isRequest() || m.isSubscribe()) {
                    //答复
                    s.replyEnd(m, new StringEntity("And you too."));
                }
            }))
        .open();


const entity = SocketD.newEntity("Hello wrold!").metaPut("sender","noear");

//发送
session.send("/demo", entity);

//发送并请求(且,等待一个答复)
session.sendAndRequest("/demo", entity).thenReply(reply=>{
    //打印
    console.info(reply);
});


//发送并订阅(且,接收零个或多个答复流)
session.sendAndSubscribe("/demo", entity).thenReply(reply=>{
    //打印
    console.info(reply);

    if(reply.isEnd()){
        console.info("the end!");
    }
});