代码拉取完成,页面将自动刷新
同步操作将从 feimat/rpc for nodejs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
'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");
}
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。