12 Star 37 Fork 10

feimat/rpc for nodejs

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
index.js 4.07 KB
Copy Edit Raw Blame History
feimat authored 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");
}
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/feimat/qiyi.git
git@gitee.com:feimat/qiyi.git
feimat
qiyi
rpc for nodejs
master

Search

23e8dbc6 1850385 7e0993f3 1850385