forked from zsxsoft/danmu-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
573 additions
and
9,641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; | ||
|
||
|
||
})(); |
Oops, something went wrong.