# rpc for nodejs **Repository Path**: feimat/qiyi ## Basic Information - **Project Name**: rpc for nodejs - **Description**: nodejs实现的rpc(基于koa实现),以网页聊天室为示例。 支持所有pc与手机浏览器 支持所有app内部webview 支持自动重连 支持心跳处理 支持双向rpc与单向通信 支持协程功能。 - **Primary Language**: NodeJS - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 36 - **Forks**: 10 - **Created**: 2016-03-22 - **Last Updated**: 2024-04-24 ## Categories & Tags **Categories**: rpc **Tags**: None ## README nodejs实现的rpc(基于koa实现),以网页聊天室为示例。 支持所有pc与手机浏览器 支持所有app内部webview 支持自动重连 支持心跳处理 支持双向rpc与单向通信 支持协程功能。欢迎使用与反馈bug。技术交流qq群:339711102 项目地址 https://git.oschina.net/feimat/qiyi 使用教程: git clone https://git.oschina.net/feimat/qiyi.git cd qiyi/ npm install 然后 node index.js 网页 http://localhost:3001/ 即可访问聊天。 发送rpc请求(这里演示客户端发送,服务器端发送参考示例): ``` var rpc_client = new rpcClient('http://‘+host); var data = "hello"; rpc_client.emit('chat', data,{ "success": function(response) { console.log(response) }, "timeout_time": 4000, "timeout_cb": function() { }, "error": function() { } }); ``` 接受rpc请求(这里演示服务器端接收,客户端接受参考示例): ``` // next:沿用koa个人感觉没必要暴露 // ctx: 对端socket上下文信息 // message: 接收到的消息 // cb: 回调函数 cb(data) 表示返回data给发送端 app.io.route('chat', function* (next, ctx, message,cb) { cb("server recive "+message+" and start send to other people "); var send_suc_num = 0; // 成功收到消息的聊天成员 for (var socket in all_sockets) { // 服务器端向客户端发送rpc app.emit(socket, 'chat', message,{ "success": function(response) { console.log("suc recive num:"+send_suc_num); }, "timeout_time": 4000, "timeout_cb": function() { }, "error": function() { } }); } } ```