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

rapid-webrpc

v0.0.1

Published

Rapid-webrpc toools

Downloads

1

Readme

rapid-webrpc

基于websocket的rpc封装工具

#How to use the rapid-webrpc

示范一个简单的在server端发布方法与在client中使用发布接口的过程.

##At Server-Side

/**
 *  引入所需的rapid组成. 这里将示范一个简单的接口和rdb查询的操作接口, 
 *  所以这里使用了simple-mongodb来访问本地的mongodb
 */
require("rapid-core");
require("rapid-httpserver");
require("rapid-webrpc");
require("rapid-simple-mongo");

rapid.define({
	"config.rapid-service-sockjs":{
		prefix:"/haha/heihei"
	},
	// httpserver只做为一个文件server,没有其它内容
	"config.rapid-httpserver":{
		autoStart:true,
		mapping:[
		    {	
		    	url:"/*",
		    	resource:"/app/*"
    		}
		]
	}
});

rapid.watch("plugin.rapid-simple-mongo","plugin.rapid-service-sockjs",function(mongo,socketMgr){
	
	// 默认连接本地的mongodb.
	var db = mongo.getAgent({},{
		idle:1000 * 10
	});
	// 处理db的错误. 防止没有error handle引发的服务崩溃
	db.on("error",function(err){
		log.err(err);
	})
	
	/**
	 * addPoint方法
	 * 
	 * addPoint(factory,description,name);
	 * 
	 * factory是服务像,上面的方法将被发布为对像的接口.
	 * description是一个描述factory的对像,用来精确描述接口的参数及返回内容. 
	 * name,表示发布接口的名称, 如果description中未提供name,则这个参数升效,否则以description中的为准.
	 * 
	 * 如果未提供description与name, 方法将自动扫描factory的结构,自动生成一个description对像,以及一个随机的名称.
	 * 这个随机的名称,在每次服务启动的时候都会不同.所以,请小心.
	 */
	// 添加一个简单的服务接口
	 socketMgr.addPoint({
		 // 以下划线开头的,直接认为是内部方法,不会向外暴露
		 _notadd:function(){},
		 // 如果没有参数,也没有callback,认为是一个notify方法,只调用,但是并不确认是否执行成功
		 what:function(){
			 console.log(arguments);
		 },
		 // 如果有一个callback,由认为是正常的通迅方法,
		 hello:function(cb){
			 cb && cb(null, "what,what,what???");
		 },
		 // 带参数的方法
		 say:function(name,cb){
			 cb && cb(null, name + " say, what is that???");
		 }
	 },null,"test");
	 
	 // 添加另一个服务接口,这个接口将做一个简单的db查询.
	 socketMgr.addPoint({
		 // 只有一个简单的方法,用来查
		query:function(name,selector,cb){
			name = name || "test";
			selector = selector || {};
			
			db.find(name,selector,function(err,rs){
				cb(err,rs);
			});
		}
	 },null,"db");
	 
});

##At Browser-Side

首选需要将client-side目录下的 "rapid-webrpc.js" 放至到能从browser中访问的位置.

<!-- 引入两个依赖的库 -->
<script type="text/javascript" src="./socketjs.js"></script>

<script type="text/javascript" src="./rapid-webrpc.js"></script>

<script type="text/javascript">

// 指定连接位置并给出一个client的名称
RapidClient.setup("/haha/heihei","testclientid" + (Date.now() / 1000));

// 处理一系统的全局事件, error, close, online, offline, unknowMsg 
RapidClient.on("error",function(err){
	console.log(error);
});

RapidClient.on("close",function(reason){
	console.log(reason);
});

RapidClient.on("unknownMsg",function(reason){
	console.log(reason);
});

//通过getProxy获得一个后端方法的代理对像
RapidClient.getProxy("test",function(err,test){
	
	if(err){
		console.log(err.stack);
		return;
	}
	
	// 直接使用.
	test.what();
	
	test.say("client ",function(err,rv){
		console.log(rv);
	});
	
	test.hello(function(err,rv){
		console.log(err,rv);
	});
});

RapidClient.getProxy("db",function(err,db){
	
	if(err){
		console.log(err.stack);
		return;
	}
	
	if(db){
		db.query("demo",{},function(err,rs){
			console.log(err,rs);
		});
	}
});

</script>