Skip to content

Commit

Permalink
从头重写项目
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxsoft committed Mar 15, 2015
1 parent ee477a7 commit e07031a
Show file tree
Hide file tree
Showing 42 changed files with 573 additions and 9,641 deletions.
5 changes: 0 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
Expand Down
116 changes: 58 additions & 58 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

# Users Environment Variables
.lock-wscript

# =========================
# Operating System Files
# =========================

# OSX
# =========================

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

Expand All @@ -54,22 +51,25 @@ Icon
Network Trash Folder
Temporary Items
.apdisk

# Windows
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
Barrage Server
danmu-server
================

a barrage server for [https://github.com/zsxsoft/screen-control](screen-control) project
弹幕服务器

一个弹幕服务器,用于[https://github.com/zsxsoft/screen-control](screen-control)这个项目。
搭配项目:

- [danmu-client](https://github.com/zsxsoft/danmu-client)
- [screen-controller](https://github.com/zsxsoft/screen-controller)

## Contributors
zsx(http://www.zsxsoft.com)

## Libraries

* jQuery [https://github.com/jquery/jquery](https://github.com/jquery/jquery)
* Bootstrap [https://github.com/twbs/bootstrap](https://github.com/twbs/bootstrap)


* express [https://github.com/strongloop/express](https://github.com/strongloop/express)
* socket.io [https://github.com/Automattic/socket.io](https://github.com/Automattic/socket.io)
* ejs [https://github.com/visionmedia/ejs](https://github.com/visionmedia/ejs)
* rich-console [https://github.com/keven-wang/rich-console](https://github.com/keven-wang/rich-console)
75 changes: 15 additions & 60 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,19 @@
"use strict";
delete require.cache['./config'];
// Include modules
var
express = require('express'),
http = require('http'),
path = require('path'),
common = require('./lib/common'),
console = require('./lib/console'),
expressLess = require('express-less');
(function() {
"use strict";

var config = require('./config').config,
httpPage = require('./' + config.webServer.serverFolders),
app = express(),
lang = require('./lang/' + config.lang).lang;
var async = require("async");
var config = global.config = require('./config');
// 公用函数
global.utils = require("./utils");

httpPage.config = config;
httpPage.lang = lang;
// 事件处理
global.coordinator = new (require('events').EventEmitter);

app
// Dev mode
/*
.use(express.logger('dev'))
.use(express.errorHandler())
*/

// set ejs
.engine('.html', require('ejs').__express)
.set('view engine', 'html')
// set ejs render
.set('views', path.join(__dirname, config.webServer.htmlFolders))
// 加载模块
async.each(["./child", "./http", "./socket", "./check"], function(module, callback) {
require(module).init(callback);
}, function(err) {
log.log("Init completed!");
});

// set less
.use('/less', expressLess(path.join(__dirname, config.webServer.staticFolders, '/less')))

.use(express.json())
.use(express.urlencoded())
.use(express.methodOverride())
.use(app.router)

// set port
.set('port', config.webServer.port)

// set static resouces
.use(express.static(path.join(__dirname, config.webServer.staticFolders)))

// set url
.get('/', httpPage.index)
.get('/manage', httpPage.manage)

;


var httpServer = http.createServer(app).listen(config.webServer.port, function(){
console.success(lang.console.serverCreated.replace("%u%", "http://127.0.0.1:" + config.webServer.port + '/'));
});

common.setConsole(console)
.setLang(lang)
.rebuildConfig(config)
.bindEvent('receiver')
.bindEvent('manage')
.bindEvent('index')
.bindServer(httpServer);
})();
24 changes: 24 additions & 0 deletions check/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(function() {
module.exports = {
init: function(callback) {
if (config.danmu.check) {
coordinator.on("httpCreated", function(app) {
// 创建关于弹幕审核器的路由
});

coordinator.on("danmuChecked", function(data) {
coordinator.emit("danmuBeforeTransfer", data);
});
}
coordinator.on("gotDanmu", function(data) {
if (config.danmu.check) {
coordinator.emit("danmuBeforeCheck", data);
} else {
coordinator.emit("danmuBeforeTransfer", data);
}
});

callback(null);
}
};
})();
81 changes: 81 additions & 0 deletions child/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(function() {
"use strict";

var io = null;
var danmuQueue = [];
var interval = 0;
var async = require("async");


global.coordinator = new(require('events').EventEmitter); // 子线程也需要事件处理
global.utils = require("../utils");
global.config = {};

process.on("message", function(data) {
coordinator.emit(data.method, data);
});

// 初始化线程
coordinator.on("init", function(data) {
coordinator.emit("configUpdated", data);
initThis(function() {
process.send({
method: "init"
});
});
});

// 更新配置
coordinator.on("configUpdated", function(data) {
config = data.data;
initTimeval();
});

// 待推送弹幕
coordinator.on("danmuTransfer", function(data) {
// 过老弹幕没有意义,直接从队列头出队列
while (danmuQueue.length > config.danmu.length) {
danmuQueue.shift();
}
var comment = {
text: data.data.text,
lifeTime: (parseInt(Math.random() * 10) + data.data.text.length % 10) * 60
};
log.log("得到弹幕(" + data.data.hash + "):" + data.data.text.substr(0, 10) + "...");
danmuQueue.push(comment);
});

// 待审核弹幕
coordinator.on("danmuBeforeCheck", function(data) {

});

var initTimeval = function() {
clearInterval(interval);
interval = setInterval(function() {
// 定时推送
var ret = [];
if (danmuQueue.length === 0) return;
while (ret.length < config.websocket.singlesize && danmuQueue.length > 0) {
ret.push(danmuQueue.pop());
}
log.log("推送" + ret.length + "条弹幕到客户端,剩余" + danmuQueue.length + "条。");
process.send({
method: "danmu",
data: ret
});
}, config.websocket.interval);
};

var initThis = function(callback) {
// 数据库由子线程负责操作
async.each(["./../database"], function(module, callback) {
require(module).init(callback);
}, function(err) {
log.log("Child process init completed!");
callback(null);
});
};


})();
Loading

0 comments on commit e07031a

Please sign in to comment.