socket.d.js v2.3.4 支持“微信“、“uniapp“

Socket.D

是基于"事件"和"语义消息""流"的网络应用层协议。有用户说,“Socket.D 之于 Socket,尤如 Vue 之于 Js、Mvc 之于 Http”。支持 tcp, udp, ws, kcp 传输。协议特点可参考《官网介绍》。

Socket.D.js 兼容更新说明

是近期发布的 socket.d 协议 js client 实现。经过社区的努力,现已:

  • 兼容 h5(浏览器)
  • 兼容 uniapp(浏览器、android, ios)
  • 兼容 微信小程序(wx 接口)
  • 兼容 node.js

Socket.D.js 能力演示:

  • 监听(相当于 ws 的增强)

多了事件路由。可以用一个连接,监听不同的业务事件(类似于 http path)。

//打开客户端会话(用 url 形式打开)
let session = await SocketD.createClient("sd:ws://127.0.0.1:8602/?token=1b0VsGusEkddgr3d")
        .listen(SocketD.newEventListener()
                .doOnOpen(s -> { //会话打开时
                    //...
                }).doOnMessage((s, m) -> { //收到任意消息时
                    //打印
                    console.info(m);
                }).doOn("/demo", (s, m) -> { //收到"/demo"事件的消息时
                    if (m.isRequest() || m.isSubscribe()) {
                        //答复
                        s.replyEnd(m, SocketD.newEntity("And you too."));
                    }
                }))
        .open();
  • 发送 和 发送文件(并获取进度)

发送相对于 ws 多了元信息。可为数据添加额外的业务标注。发送大数据时,会自动分片(接收端自动聚合)

//发送
session.send("/demo/hello", SocketD.newEntity("hi").metaPut("sender","noear"));

//发送文件,且获取发送进度(如果有大数据发送,又需要显示进度)//实际开发,要用 sendAndRequest 接口(以获取接收确认)
session.send("/demo/upload", SocketD.newEntity(file)).thenProgress((isSend, val, max)=>{
    if(isSend){
        //获取发送进度
        console.info(`...${val}/${max}`);
    }
});
  • 请求 和 下载文件(或大数据块)

这个相当于 ws 有了 ajax 的交互方式

//发送并请求(有点像 ajax)
let reply = session.sendAndRequest("/demo/hello", SocketD.newEntity()).thenReply(reply=>{
    console.info(reply.dataAsString());
});

//发送并请求,且取接收进度(如果有大数据获取,又需要显示进度)
session.sendAndRequest("/demo/download", SocketD.newEntity()).thenProgress((isSend, val, max)=>{
    if(!isSend){
        //获取接收进度
        console.info(`...${val}/${max}`);
    }
}).thenReply(reply=>{
      //reply.data()...
}).thenError(err=>{
      //如果有出错?
});
  • 订阅 与 流量控制(一般用于流加载)

通过 range(start, size) 指定数据范围,由 sendAndSubscribe 发起订阅,通过 thenReply 多次接收。

//发送并订阅
let entity = SocketD.newEntity().range(5,5).metaPut("videoId","1");
session.sendAndSubscribe("/demo/stream", entity).thenReply(reply=>{
      //异步获取答复(会多次回调)
})

视频演示效果:

  • for H5
    • https://www.bilibili.com/video/BV1ck4y197BV/
    • 演示源码
  • for UniAPP
    • https://www.bilibili.com/video/BV1T94y1T7sp/
    • 演示源码

代码仓库:

  • https://gitee.com/noear/socketd
  • https://github.com/noear/socketd

官网:

  • https://socketd.noear.org

你可能感兴趣的:(Socket.D,网络应用协议,javascript,微信,uni-app)