Skip to content

Socket.D v2.3.9

Compare
Choose a tag to compare
@noear noear released this 30 Jan 05:08
· 548 commits to main since this release

java 适配更新:

  • 无更新

javascript 适配更新:

  • 完成 for Node.js server 实现
  • 添加 Session::remoteAddress,localAddress 方法

变更示例:

for Node.js demo

  • for package.json
{
  "name": "demo",
  "description": "socket.d for node.js demo",
  "author": "noear",
  "dependencies": {
    "@noear/socket.d": "2.3.9",
    "ws": "^8.16.0"
  }
}
  • for ClientTest.js(这是之前支持的)
const {SocketD}  = require('@noear/socket.d');

async function main() {
    let clientSession = await SocketD.createClient('sd:ws://127.0.0.1:8602/?u=a&p=2')
        .config(c => c.fragmentSize(1024 * 1024))
        .listen(SocketD.newEventListener().doOnMessage((s, m) => {
            console.log('收到推送', m.dataAsString());
        }))
        .open();
    clientSession.sendAndRequest("/demo", SocketD.newEntity('hello')).thenReply(reply => {
        console.log('reply', reply);
    });
}

main();
  • for ServerTest.js(这是刚刚支持的)
const {SocketD}  = require('@noear/socket.d');

function main(){
   let server = SocketD.createServer("sd:ws")
       .config(c=>c.port(8602).fragmentSize(1024 * 1024))
       .listen(buildListener())
       .start();
}

function buildListener() {
    return SocketD.newEventListener()
        .doOnOpen(s => {
            console.info("onOpen: " + s.sessionId());
        }).doOnMessage((s, m) => {
            console.info("onMessage: " + m);
        }).doOn("/demo", (s, m) => {
            if (m.isRequest()) {
                s.reply(m, SocketD.newEntity("me to!"));
            }

            if (m.isSubscribe()) {
                let size = m.rangeSize();
                for (let i = 1; i <= size; i++ ) {
                    s.reply(m, SocketD.newEntity("me to-" + i));
                }
                s.replyEnd(m, SocketD.newEntity("welcome to my home!"));
            }
        }).doOn("/upload", (s, m) => {
            if (m.isRequest()) {
                let fileName = m.meta(SocketD.EntityMetas.META_DATA_DISPOSITION_FILENAME);
                if (fileName) {
                    s.reply(m, SocketD.newEntity("no file! size: " + m.dataSize()));
                } else {
                    s.reply(m, SocketD.newEntity("file received: " + fileName + ", size: " + m.dataSize()));
                }
            }
        }).doOn("/download", (s, m) => {
            if (m.isRequest()) {
                let fileEntity = SocketD.newEntity("...");//todo://SocketD.newEntity(fs.readFileSync("/Users/noear/Movies/snack3-rce-poc.mov"));
                s.reply(m, fileEntity);
            }
        }).doOn("/push", (s, m) => {
            if (s.attrHas("push")) {
                return;
            }

            s.attrPut("push", "1");

            for (let i = 0; i++; i < 100) {
                if (s.attrHas("push") == false) {
                    break;
                }

                s.send("/push", SocketD.newEntity("push test"));
                //todo:sleep
            }
        }).doOn("/unpush", (s, m) => {
            s.attrMap().remove("push");
        })
        .doOnClose(s => {
            console.info("onClose: " + s.sessionId());
        }).doOnError((s, err) => {
            console.warn("onError: " + s.sessionId());
        });
}

main();