0.0.2 • Published 2 years ago

ws-bi-rpc v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

WS-BI-RPC

Bidirectional RPC based on WebSocket.

Easy to use

import { WebSocketServer,WebSocket ,WebSocketRPC } from "..";

class ServerHander {
    say(message: string) {
        console.log('message from client:', message)
    }
}
class ClientHander {
    test(message: string) {
        console.log('message from server :', message)
    }
}
const server = new WebSocketServer({ host: 'localhost', port: 8899 });
server.on('connection', async (socket) => {
    const rpc = new WebSocketRPC<ClientHander>(socket, new ServerHander);
    await rpc.call('test', 'hello');
});


const ws = new WebSocket('ws://localhost:8899');

ws.on('open', async () => {
    const client = new WebSocketRPC<ServerHander>(ws, new ClientHander);
    await client.call('say', 'say message');
})


// message from server : hello
// message from client: say message