Creates the Client instance
An object which passed out to define all the main properties of the server. Notable fields are: ClientOptions.address, ClientOptions.prepareContext.
Fires when the connection with the server closes
Fires when the client successfully reconnects to the server
Fires when the server refuses the client to connect in the reconnection attempt
Call the server method using params as one argument construction.
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.
Establish tho connection to the server using an address and unique id that specifies the client's constructor.
Register the method on the client side, handler must return an object or a Promise<object> which will be held on the server side
Additional method to remove all listeners. See https://html.spec.whatwg.org/multipage/web-sockets.html#garbage-collection-2
Remove them only from the websocket instance
Generated using TypeDoc
A wrapper around the Client class. It has almost the same methods and events as the Client class, with the exception of the connection sequence. So please refer documentation to the Client class for the similar methods.
The connection sequence looks like follows:
const client = new ReconnectingClient() // creates an instance 🠗 client.register('method1', handler1); await client.init() client.register('method2', handler2); // inits the connection: // ⌛ client now tries to connect to the server 🠗 // when the client establishes a websocket connection, it can // 🛑 reject the promise and emits `connectError` event if server refuses // the client to connect with its token. Thus it stops any connection // attempts until you execute `init` method again. // 👌 resolve the promise and emits `connect` event if the server accepts // the connection. 🠗 await client.call('serverMethod' {...args}); client.register('method3', handler3); // ⚡ if everything is 👌, here you can call server's methods. Note that when // the client isn't connected every execution of the `call` method will // throw an error. 🠗 client.addEventListener('connect', () => {console.log('😃')}); client.addEventListener('connectError', () => {console.error('😟')}); // 💥 when the connection to the server breaks, an `close`event will be // raised // ⌛ and the client will try to connect to the server again // At this point you can check the connection status via `connect` or // `connectError` events. // And, as you can see, you can register client rpc-methods everywhere in // the code after calling the constructor. And this methods along with // the events will be connected to every new inner instance of the // Client class
This class can register any method to call it by the server. And also can call any method on the server and receive response data. And there are some events that can help you.
The main parts of this class are:
import { ReconnectingClient } from '@noveo/dual-rpc-ws/client'; const client = new ReconnectingClient({ token: 'id13', address: 'ws://192.168.0.80:8080' }); try { await client.init(); console.log('successfully connected for the first time!'); client.register('hi', (params, context) => { console.log('client hi, call id:', context.id); return Promise.resolve(`${token}, hello`); }); client.addEventListener('connect', () => { console.log('successfully reconnected!'); }); client.addEventListener('connectError', () => { console.error('server refuses to reconnect!'); }); client.addEventListener('close', () => { console.log('connection closed!'); }); try { const serverResponse = client.call('hi', {message: 'Server, hi!'}); console.log('Hello from server', serverResponse); } catch(e) { console.error('Trouble with call', e.message); } } catch(e) { console.error('server refuses to connect!') }