Git Product home page Git Product logo

Socket.D

基于语义消息流的网络开发框架

Maven Apache 2 jdk-8 jdk-11 jdk-17 jdk-21
gitee star github star



Socket.D 是一种应用协议,也是一个开发框架。可以在客户端和服务端之间“快速”、“高质量”、“流式”自由通讯。

主要特性

  • 异步通讯,由带语义的主题消息驱动
  • 语言无关,使用二进制通信协议(支持 tcp, ws, udp)。支持多语言、多平台
  • 背压流控,请求时不让你把服务端发死了
  • 断线重连,自动连接恢复
  • 多路复用
  • 双向通讯,单链接双向互听互发
  • 自动分片,数据超出 16Mb,会自动分片、自动重组(udp 除外)
  • 扩展定制,可为数据添加 meta 语义标注(就像 http header)
  • 接口简单

与 http、websocket 的简单对比

对比项目 socket.d http websocket(ws) 备注
发消息(Qos0) 适合监听埋点,日志上报
发送并请求(Qos1) 适合马上答复确认
发送并订阅(流) 适合视频播放之类的,分块流式获取
答复或响应
单连接双向通讯 有(不便) 双向互发、互听。适合反向调服务
数据分片 / 适合大文件上传
断线自动重连 /
有元信息或头信息
基础传输协议 tcp, udp, ws tcp http

适用场景

可用于 MSG、RPC、IM、MQ 等一些的场景开发,可替代 Http, Websocket, gRpc 等一些协议。比如移动设备与服务器的连接,比如一些微服务场景等等。

简单的协议说明( 详见:《协议文档》

  • 连接地址风格
sd:tcp://19.10.2.3:9812/path?u=noear&t=1234
sd:udp://19.10.2.3:9812/path?u=noear&t=1234
sd:ws://19.10.2.3:1023/path?u=noear&t=1234
  • 帧码结构
//udp only <2k
[len:int][flag:int][sid:str(<64)][\n][topic:str(<512)][\n][metaString:str(<4k)][\n][data:byte(<16m)]
  • 指令流
Flag Server Client
Unknown ::close() ::close()
Connect / c(Connect)->s::onOpen(),s(Connack?)->c::onOpen()
Connack ->s::onOpen(),s(Connack?)->c /
Ping / c(Ping)->s(Pong)->c
Pong ->s(Pong)->c /
Close s(Close)->c c(Close)->s
Message s(Message)->c c(Message)->s
Request s(Request)->c(Reply or ReplyEnd)->s c(Request)->s(Reply or ReplyEnd)->c
Subscribe s(Subscribe)->c(Reply...ReplyEnd)->s c(Subscribe)->s(Reply...ReplyEnd)->c
Reply ->s(Reply)->c ->c(Reply)->s
ReplyEnd ->s(ReplyEnd)->c ->c(ReplyEnd)->s
//The reply acceptor registration in the channel is removed after the reply is completed

快速入门与学习

  • 学习

请点击:《快速入门与学习》。Java 之外的语言与平台会尽快跟进(欢迎有兴趣的同学加入社区)

  • 规划情况了解
语言或平台 客户端 服务端 备注
java 已完成 已完成 支持 tcp, udp, ws 通讯架构
js 开发中 / 支持 ws 通讯架构
python 开发中 / 支持 ws 通讯架构
其它 计划中 计划中

加入到社区交流群

QQ交流群:870505482 微信交流群(申请时输入:SocketD)

交流群里,会提供 "保姆级" 支持和帮助。如有需要,也可提供技术培训和顾问服务

第一个程序:你好世界!

public class Demo {
    public static void main(String[] args) throws Throwable {
        //::启动服务端
        SocketD.createServer(new ServerConfig("sd:tcp").port(8602))
                .listen(new SimpleListener(){
                    @Override
                    public void onMessage(Session session, Message message) throws IOException {
                        if(message.isRequest()){
                            session.replyEnd(message, new StringEntity("And you too."));
                        }
                    }
                })
                .start();

        Thread.sleep(1000); //等会儿,确保服务端启动完成
        
        //::打开客户端会话
        Session session = SocketD.createClient("sd:tcp://127.0.0.1:8602/?token=1b0VsGusEkddgr3d")
                .open();
        
        //发送并请求(且,收回答复)
        Entity reply = session.sendAndRequest("/demo", new StringEntity("Hello wrold!").meta("user","noear"));
    }
}

anpetal's Projects

admin4j-framework icon admin4j-framework

开箱即用的SpringBoot starter项目,抽离业务层,封装了常规业务所需的框架功能,可自由组合各个模块,让业务更加组件化开发。

dyruntime icon dyruntime

一款基于iOS的用于记录用户运动轨迹的App

lean4 icon lean4

Lean 4 programming language and theorem prover

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.