npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonrpcv2

v0.3.2

Published

JSON-RPC 2.0 Library for JavaScript

Downloads

12

Readme

Node.js CI Node.js Package

json-rpc

JSON-RPC 2.0 协议的JavaScript实现,兼容Node和Web

  • 弱化client/server,任何一端都可以主动发出request和notification
  • 提供简单、易用的Promise接口
  • 不仅限于Web/Node,如:WebView/Native
  • 解析JSON-PRC 2.0数据报文,提供方法暴露、调用等接口

安装和使用

npm i jsonrpcv2

使用

服务端

import { WebSocketServer } from 'ws';
import { RPC } from 'jsonrpcv2';

const serverRPC = new RPC();

// 注册同步方法
serverRPC.expose('syncMethod', () => {
    return 'syncMethod.result';
});

// 注册异步方法
serverRPC.expose('asyncMethod', () => {
    return Promise.resolve('asyncMethod.result');
});

const wss = new WebSocketServer({
    port: 2188
});

wss.on('connection', (websocket) => {
    // rpc通过websocket发送消息
    serverRPC.setTransmitter((message) => {
        websocket.send(message);
    });
    // websocket接受到消息转发给rpc
    websocket.on('message', (message) => {
        serverRPC.receive(Buffer.from(message).toString());
    });
});

WebView

import { RPC } from 'jsonrpcv2';

const clientRPC = new RPC();

// web的websocket原生对象
const ws = new WebSocket('localhost:2188');
ws.onopen = function () {
    // 调用服务端syncMethod方法
    clientRPC.call('syncMethod')
        .then((result) => {
            console.log(result); // syncMethod.result
        })
        .catch((error) => {
            // error
        });

    // 调用服务端 asyncMethod 方法
    clientRPC.call('asyncMethod')
        .then((result) => {
            console.log(result); // asyncMethod.result
        })
        .catch((error) => {
            // error
        })
}

API

rpc = new RPC()

import { RPC } from 'jsonrpcv2'
const rpc = new RPC();

rpc.receive(message)

对端的RPC消息通过该方法通知到本地RPC

  • message {string} JSON RPC报文

rpc.setTransmitter(transmitter)

到对端的RPC消息从这里发出,消息如何放松到对端,取决于你的transmitter

  • transmitter {(message:string)=>Promise} 发送通道

rpc.expose(methodName, method)

暴露方法,供对端调用

  • methodName {string} 暴露的方法名称
  • method {Function} 方法实例

    根据JSON-RPC 2.0 标准,暴露方法的参数支持索引和名称关联, jsonrpcv2目前暂时仅支持索引

rpc.unexpose(methodName)

取消已暴露的方法

  • methodName {string} 已暴露的方法名称

rpc.call(methodName,args)

调用对端方法

  • methodName {string} 方法名称
  • args {Array | Object} 参数,索引(Array)或名称关联(Object)
  • return {Promise<Result>}

rpc.onNotification(name, callback)

添加通知监听

  • name {string} 通知名称
  • callback {Function} 通知回调

rpc.removeNotification(name, callback)

删除通知监听

  • name {string} 通知名称
  • callback {Function} 通知回调

rpc.notify(name,args)

通知对端

  • name {string} 通知名称
  • args {Array | Object} 参数,索引(Array)或名称关联(Object)