elpheria/rpc-websockets

client also need register rpc method

Opened this issue · 5 comments

wizd commented
server.register(method, handler[, namespace]) -> RPCMethod

this should be available to client also to support server callback.

Hi, @wizd, thanks for the comment. This one is part of the v6 which isn’t released yet. You can try testing that release, it’s up on npm! #29

@wizd have you tried v6?

wizd commented

not yet but I added a server callback to make it works:

      // If this is an object with error, it is an error response.
      else if ('error' in response && this._wsCallbacks[response.id]) {
        // Get the error callback.
        var errorCb = this._wsCallbacks[response.id].errorCb;

        // Delete the callback from the storage.
        delete this._wsCallbacks[response.id];

        // Run callback with the error object as parameter.
        errorCb(response.error);
        return;
      }

      // a server call back
      else if ('method' in response) {
        // Construct the JSON-RPC 2.0 result.
        var cbresult = await this.options.oncallback(response);
        var request = {
          jsonrpc: '2.0',
          id: response.id,
          result:  cbresult
        };

        if(cbresult != null) {
          // Try making a WebSocket call.
          var socket = this.options.getSocket(this.wsOnMessage);
          if (socket !== null) {
            this._wsCall(socket, request);
          }
        }

@mkozjak I am confused by this - can you confirm if v6 is live?
AFAICT we're now on v7, but I can't see how a server can call a client method?

Thanks.

Yup, v6 is beta only, in v7 we continued developing regular version since we didn't get any feedback on v6 so we ignored it.

https://www.npmjs.com/package/rpc-websockets/v/6.0.2-beta.0

PRs welcome.