1 Star 0 Fork 10

havealex/rpc for nodejs

forked from feimat/rpc for nodejs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 4.07 KB
一键复制 编辑 原始数据 按行查看 历史
feimat 提交于 2016-11-18 00:25 . first add
'use strict';
var port = 3001;//˿
var usernames = {};
var numUsers = 0;
var ctx_dic = {};
var cb_dic = {};
var staticCache = require('koa-static-cache');
var koa = require('koa.io');
var path = require('path');
var fs = require('fs');
var rpcServer = require('./rpcSvr');
var app = new rpcServer();
// Routing
app.use(staticCache(path.join(__dirname, 'public')));
app.use(function*() {
this.body = fs.createReadStream(path.join(__dirname, 'public/index.html'));
this.type = 'html';
});
app.listen(port, function () {
console.log('Server listening at port %d', port);
});
app.setBeatTime(5000);
app.io.use(function* (next, ctx) {
// on connect
console.log("connect");
ctx_dic[ctx.socket.id] = ctx;
console.log("socket size:"+Object.keys(ctx_dic).length);
yield next;
// on disconnect
delete ctx_dic[ctx.socket.id];
console.log('disconnect');
console.log("socket size:"+Object.keys(ctx_dic).length);
if (ctx.addedUser) {
delete usernames[ctx.username];
--numUsers;
}
});
app.io.route('adduser', function* (next,ctx,username) {
ctx_dic[ctx.socket.id] = ctx;
ctx.username = username;
usernames[username] = username;
++numUsers;
ctx.addedUser = true;
ctx.emit('login', {
numUsers: 1
});
ctx.broadcast.emit('user joined', {
username: ctx.username,
numUsers: numUsers
});
//var userData = yield mysql.query("insert into mh_user(regip,nickname) values(?,?)",['192.168.1.158','zhangsan']);
//console.log('idΪ'+userData.insertId);
});
function stat(file) {
return function (done) {
fs.stat(file, done);
};
}
var thunkify = require('thunkify');
var read = thunkify(fs.readFile);
//var c = yield read('/root/koaio/a.txt');
//console.log(c.toString());
app.io.route('chat', function* (next, ctx, message,cb) {
//if (message.message == "exit") {
// process.exit();
//}
if (message.message == "nimab") {
return;
}
var send_msg = {
"username": message.username,
"message": message.message
}
var online_num = Object.keys(ctx_dic).length;
if (online_num == 1) {
if (cb) cb("only youself");
}
//var suc_count = 0;
//var err_count = 0;
//var all_suc_name = "";
//var all_offline_name = "";
for (var key in ctx_dic) {
if (key != ctx.socket.id) {
var other_ctx = ctx_dic[key];
(function(){
var cur_ctx = other_ctx;
var username = other_ctx.username;
app.emit(other_ctx,'chat',send_msg,{
//"success": function*(){
// all_suc_name += username+" ";
// if (++suc_count == (online_num-1)) {
// if (cb) cb("send to "+all_suc_name+" success");
// }
//},
//"timeout_time": 2000,
//"timeout_cb": function*(){
// //cur_ctx.disconnect();
// ++err_count;
// all_offline_name += username+" ";
// if ((suc_count+err_count) == (online_num-1)) {
// if (cb) cb(all_offline_name+" offline");
// }
//},
//"error": function*() {
// //cur_ctx.disconnect();
// ++err_count;
// all_offline_name += username+" ";
// if ((suc_count+err_count) == (online_num-1)) {
// //cur_ctx.disconnect();
// if (cb) cb(all_offline_name+" offline");
// }
//}
});
})();
}
}
if (cb) cb("send to all suc");
});
app.io.route('typing', function* () {
console.log('typing');
});
app.io.route('stoptyping', function* () {
console.log('stoptyping');
});
app.io.route('checkName', function* (next,ctx,msg,cb) {
if (msg in usernames) {
cb("no");
} else {
cb("ok");
}
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/havealex/qiyi.git
git@gitee.com:havealex/qiyi.git
havealex
qiyi
rpc for nodejs
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385