Setup the server with ws.ServerOptions object.
ServerOptions.handshake is the handler for the new connections.
It accepts client token, ClientSocket instance
of the new connection and returns true
if the connection can be established and false
to broke the connection.
If you don't pass the handshake
property, server will accept every client.
An object which passed out to define all the main properties of the server. Notable fields are: ServerOptions.port, ServerOptions.handshake and ServerOptions.prepareContext.
Fires when the connection with the client closed
The unique token of the client from the Server.handshake handler
Call the client's method by the token and method name using params as one argument construction.
Unique token of the client
The name of the remote method to call
Method arguments
Returns a Promise with the JSON response from the client. It can be an object, an array or a primitive.
Register the method on the server side, handler accepts token of the client
it was called and params as an object and must return an object
or a
Promise<object>
which will be held on the client side.
First argument of the handler is the context object, which contains by the default token and unique message id of the rpc call
Method name
Handler with context and params arguments
Generated using TypeDoc
Websocket server class which accepts the connection from the clients, checks their tokens. It can register any method to call it by the client. And also can call any method on the client by its token and receive response data.
The main parts of this class are:
import Server from '@noveo/dual-rpc-ws'; const server = new Server({ port: 8081, handshake: async (token) => { console.log('connected', token); this.call(token, 'hi', {message: 'hello from server'}); return Promise.resolve(true); }, }); server.register('hi', (token, params) => { console.log('server hi', params); return Promise.resolve(`${token}, hello`); });