Fork me on GitHub

node_rpc by play175

node_rpc

node_rpc是一个兼容IE6、7、8和所有浏览器的浏览器客户端和node服务端之间的通信库。

一个简单的demo,多人连连看:http://findme.aliapp.com/

Install/安装

可以直接下载代码或者在npm里面安装:
npm install node_rpc

Use/使用

Server/服务端(app.js):
  1. var app = require('node_exsvr'); 
  2. var node_rpc =  require('node_rpc'); 
  3.  
  4. //监听,默认80端口 
  5. app.listen(); 
  6.  
  7. //静态文件 
  8. app.get('/',app.staticHandler('index.html')); 
  9.  
  10. //node_rpc 
  11. node_rpc(function(client){ 
  12.    //客户端连接成功 
  13.     console.log('welcome,'+client.sessionID); 
  14.  
  15.     this.login = function(name){//方法被客户端调用 
  16.         console.log(name); 
  17.         client.hello('hello,'+name);//直接调用客户端方法 
  18.         return ({ss:'1'});//返回json对象给客户端 
  19.     }; 
  20.      
  21.     this.say = function (msg,callback) { 
  22.         console.log(msg); 
  23.         callback('you said.'); 
  24.     }; 
  25. },app); 
Client/客户端(index.html):
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.  <head>  
  4.   <title> node_rpc </title>  
  5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6.   <script type="text/javascript" src="/node_rpc.js"></script>  
  7.  </head>  
  8.  <body onload="ready()">  
  9.   <h1>node_rpc</h1>  
  10.   <script type="text/javascript">  
  11.   <!--  
  12.    
    1. function ready() {   
    2.        node_rpc(function(){   
    3.            this.hello = function(s){//将可直接被服务端调用   
    4.            console.log(s);   
    5.            };   
    6.            this.count = function(s){//将可直接被服务端调用   
    7.                console.log('count:'+s);   
    8.            };   
    9.        }).connect(function (remote) {   
    10.                //直接调用服务端   
    11.               remote.login('xxx',function(res){   
    12.                    console.log(res);//从服务端返回   
    13.                    remote.say('hello,svr',function(r){   
    14.                        console.log('say,return:'+r);   
    15.                    });   
    16.               });   
    17.        });   
    18.    }   
  13.   //-->  
  14.   </script>  
  15.  </body>  
  16. </html>  

Contact

yoyo (http://yoyo.play175.com/)

Download

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