node_rpc是一个兼容IE6、7、8和所有浏览器的浏览器客户端和node服务端之间的通信库。
一个简单的demo,多人连连看:http://findme.aliapp.com/
npm install node_rpc
Client/客户端(index.html):
- var app = require('node_exsvr');
- var node_rpc = require('node_rpc');
- //监听,默认80端口
- app.listen();
- //静态文件
- app.get('/',app.staticHandler('index.html'));
- //node_rpc
- node_rpc(function(client){
- //客户端连接成功
- console.log('welcome,'+client.sessionID);
- this.login = function(name){//方法被客户端调用
- console.log(name);
- client.hello('hello,'+name);//直接调用客户端方法
- return ({ss:'1'});//返回json对象给客户端
- };
- this.say = function (msg,callback) {
- console.log(msg);
- callback('you said.');
- };
- },app);
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title> node_rpc </title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script type="text/javascript" src="/node_rpc.js"></script>
- </head>
- <body onload="ready()">
- <h1>node_rpc</h1>
- <script type="text/javascript">
- <!--
- function ready() {
- node_rpc(function(){
- this.hello = function(s){//将可直接被服务端调用
- console.log(s);
- };
- this.count = function(s){//将可直接被服务端调用
- console.log('count:'+s);
- };
- }).connect(function (remote) {
- //直接调用服务端
- remote.login('xxx',function(res){
- console.log(res);//从服务端返回
- remote.say('hello,svr',function(r){
- console.log('say,return:'+r);
- });
- });
- });
- }
- //-->
- </script>
- </body>
- </html>
yoyo (http://yoyo.play175.com/)
You can download this project in either zip or tar formats.
You can also clone the project with Git by running:
$ git clone git://github.com/play175/node_rpc