Socket.D v2.3.7 发布
Socket.D 是基于"事件"和"语义消息""流"的网络应用层协议。有用户说,“Socket.D 之于 Socket,尤如 Vue 之于 Js、Mvc 之于 Http”。支持 tcp, udp, ws, kcp 传输。协议特点可参考《官网介绍》。 pyton 已开发完成,再在测试中;go, rust, c++ 正在开发中。 for java / for js 更新说明: 添加 Client::openOrThow() 方法,原 open() 不再出异常 添加 Session::liveTime 接口 添加 Entity.of 快捷方法 添加 连接协议对 meta 传递的支持 添加 Handshake:path 方法 添加 CodecReader::peekByte 方法,标准化字符串解码 调整 发送时允许实体为 null(总有不需要传的时候) 调整 ClientChannel 内部处理,支持首次连接失败后仍可用 简化 ClientBase::open() 处理 优化 Codec::decodeString 处理方式 新增的接口体验: 两种打开方式 //如果不能连接正常返回(由心跳尝试不断连接)//用 java 示例 let session = SocketD.createClient("sd:tcp://127.0.0.1:8602/?u=a&p=2") .open(); //如果不能连接则异常 // 用 js 示例 const session = await SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2") .openOrThow(); 支持用 null 发空实体 //等待模式 let reply = session.sendAndRequest("/user/get", null).await(); //回调模式 session.sendAndRequest("/user/get", null).thenReply(reply->{ }); 支持活动时间获取 console.info(session.liveTime()); 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