socketio-bus

支持以节点名/群组名的方式提供消息总线服务,提供数据发送确认功能以及服务器忙闲状态

Usage no npm install needed!

<script type="module">
  import socketioBus from 'https://cdn.skypack.dev/socketio-bus';
</script>

README

Server

const {server, client} = require('./index')

const busServer = new server();

busServer.init({
    host: 'localhost',
    port: 5080
})

Client-1

const {server, client} = require('./index')

const busClient = new client();
(async ()=>{
    await busClient.initAsync({
        host: 'localhost',
        port: 5080,
        nName: 'test-01'
    })

    busClient.registerName({
        nName: 'test-01'
    })

    busClient.joinGroup({
        gName: 'TEST-G'
    });
    busClient.joinGroup({
        gName: 'TEST-G-2'
    });

    busClient.on({
        eventName: 'test',
        callback: ({x})=>{
            console.log('test: ', x);
        }
    })

    busClient.sub({
        topic: 'mysub',
        callback: ({x})=>{
            console.log(x);
        }
    })

    setTimeout(()=>{
        busClient.toGroup({
            gName: 'TEST-G',
            gEventName: 'test',
            x: 'toGroup:1'
        })

        busClient.toNode({
            nName: 'test-02',
            nEventName: 'test',
            x: 'toNode:2'
        })
        busClient.toNodes({
            nNameArr: ['test-01', 'test-02'],
            nEventName: 'test',
            x: 'toNodes:22'
        })

    }, 5000)

})()

Client-2

const {server, client} = require('./index')

const busClient = new client();
(async ()=>{
    await busClient.initAsync({
        host: 'localhost',
        port: 5080,
        nName: 'test-02'
    })

    busClient.registerName({
        nName: 'test-02'
    })

    busClient.joinGroup({
        gName: 'TEST-G',
        callback: (ackData)=>{
            console.log('callback: ', ackData);
        }
    });

    busClient.on({
        eventName: 'test',
        callback: ({x})=>{
            console.log('test: ', x);
        }
    })

    setTimeout(()=>{
        
        busClient.toGroup({
            gName: 'TEST-G',
            gEventName: 'test',
            x: 'toGroup:11'
        })

        busClient.toGroups({
            gNameArr: ['TEST-G', 'TEST-G-2'],
            gEventName: 'test',
            x: 'toGroups:22'
        })

        busClient.pub({
            topic: 'mysub',
            x: 'mysub:888'
        })
    }, 5000)
    
})()