Skip to content

Commit

Permalink
添加指定路径支持
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Jul 12, 2013
1 parent 06ff447 commit 2683cae
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 78 deletions.
5 changes: 0 additions & 5 deletions assets/css/nodeppt.css
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,6 @@ slides > slide.dark {
/* line 252, ../scss/nodeppt.scss */
slides > slide.title-slide:after {
content: '';
background: url(../../images/io2012_logo.png) no-repeat 100% 50%;
-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;
position: absolute;
bottom: 40px;
right: 40px;
Expand Down
4 changes: 2 additions & 2 deletions assets/scss/nodeppt.scss
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ slides > slide {
&.title-slide {
&:after {
content: '';
background: url(../../images/io2012_logo.png) no-repeat 100% 50%;
@include background-size(contain);
//background: url(../../images/io2012_logo.png) no-repeat 100% 50%;
//@include background-size(contain);
position: absolute;
bottom: $slide-top-bottom-padding;
right: $slide-top-bottom-padding;
Expand Down
4 changes: 3 additions & 1 deletion bin/nodeppt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ var nodePPT = require(join(root, './lib/nodePPT'));

var opts = {
'-v': '--version',
'-p': '--port'
'-p': '--port',
'-d': '--dir'
}

var argv = parseArgs(process.argv);
var action = argv.slice(2, 3).toString();

if (typeof nodePPT[action] === 'function') {
nodePPT.cwd = process.cwd();

nodePPT[action](argv.slice(3));
} else {
nodePPT.help();
Expand Down
30 changes: 24 additions & 6 deletions lib/nodePPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var templateQ = [{
var ppt = module.exports = {
help: function() {

console.log(' ________________|'.bold.green+'nodePPT帮助'.bold.red+'|_______________'.bold.green);
console.log(' ________________|'.bold.green + 'nodePPT帮助'.bold.red + '|_______________'.bold.green);
console.log(' | |'.bold.green);
console.log(' | nodeppt start: 启动服务 |'.bold.green);
console.log(' | nodeppt start -p 8000: 启动8000端口 |'.bold.green);
Expand All @@ -40,20 +40,38 @@ var ppt = module.exports = {
start: function(args) {
//启动
var port = 8080;
var dir;
var self = this;
for (var i = 0, len = args.length; i < len; i++) {
if (args[i] === '--port') {
port = args[i + 1] ? args[i + 1] : 8080;
break;
switch (args[i]) {
case '--port':
port = args[i + 1] ? args[i + 1] : 8080;
i++;
break;
case '--dir':
dir = args[i + 1] ? path.join(self.cwd, args[i + 1]) : null;
i++;
break;
}
}
require(libDir + '/server').start(port);

if (!fs.existsSync(dir)) {
return console.log('\nERROR: '.bold.red + dir + ' 不是一个正确路径');
} else {
var stat = fs.statSync(dir);
if (!stat.isDirectory()) {
return console.log('\nERROR: '.bold.red + dir + ' 不是一个正确路径');
}
}

require(libDir + '/server').start(port, dir);
},
create: function(args) {
var opts = {};
var filename = args.slice(-1).toString();
if (filename !== '') {
if (gFile.exists(rootDir + '/ppts/' + filename + '.html')) {
console.log('ERROR: '.bold.red+'"'+filename+'" 已经存在!');
console.log('ERROR: '.bold.red + '"' + filename + '" 已经存在!');
}
opts.filename = filename;
templateQ.splice(0, 1);
Expand Down
126 changes: 64 additions & 62 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ var storeMemory = new MemoryStore({
reapInterval: 60000 * 10
});

module.exports.start = function(port) {
module.exports.start = function(port, pptDir) {
port = parseInt(port, 10) || 8080;
var app = startApp(port);

var app = startApp(port, pptDir);
var io = require('socket.io').listen(app, {
// log: false,
origins: '*:*' //解决同源策略
Expand Down Expand Up @@ -51,17 +52,17 @@ module.exports.start = function(port) {
var session = socket.handshake.session; //session
// var uid = now++;
var uid = session.uid;

socket.uid = uid;
console.log('socket.io:'+uid);
console.log('socket.io:' + uid);
sockets[uid] = socket;
//监听添加map
socket.on('add client', function(data) {
var targetUid = socket.targetUid = data.targetUid;
var otherSocket = sockets[targetUid];
userMap[targetUid] = uid;
userMap[uid] = targetUid;

otherSocket && otherSocket.emit('system', {
action: 'join',
joinUid: uid
Expand Down Expand Up @@ -96,73 +97,74 @@ module.exports.start = function(port) {

}

function startApp(port) {
function startApp(port, dir) {
var staticDir = path.join(__dirname, '../assets') + '/';
var pptDir = path.join(__dirname, '../ppts') + '/';
var pptDir = (dir || path.join(__dirname, '../ppts')) + '/';
console.log('路径:' + pptDir);
var now = Date.now();
var app = connect(
connect.cookieParser(),
connect.session({
secret: 'wyq',
store: storeMemory
}),

function(req, res) {
var url = URL.parse(req.url).path;
var dirname = path.dirname(url);
var realPath, ext;
if (dirname === '/ppt') {

var uid = req.session.uid;
if (!uid) {
uid = now++; //当前连接用户的UID
req.session.uid = uid;
console.log('new user: ' + uid);
} else {
console.log('welcome back ' + uid);
}

connect.cookieParser(),
connect.session({
secret: 'wyq',
store: storeMemory
}),

function(req, res) {
var url = URL.parse(req.url).path;
var dirname = path.dirname(url);
var realPath, ext;
if (dirname === '/ppt') {

var uid = req.session.uid;
if (!uid) {
uid = now++; //当前连接用户的UID
req.session.uid = uid;
console.log('new user: ' + uid);
} else {
console.log('welcome back ' + uid);
}

realPath = pptDir + path.basename(url, '.html') + '.html';
ext = 'html';
} else {
realPath = staticDir + url;
ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
}

realPath = pptDir + path.basename(url, '.html') + '.html';
ext = 'html';
} else {
realPath = staticDir + url;
ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
}

//静态资源
if (fs.existsSync(realPath)) {
fs.readFile(realPath, 'binary', function(err, file) {
if (err) {
res.writeHead(500, {
'Powered-By': 'nodePPT',
'Content-Type': 'text/plain'
});
res.end(err);
} else {
res.writeHead(200, {
'Powered-By': 'nodePPT',
'Content-Type': mimes[ext]
});
res.write(file, 'binary');
res.end();
}

});
//静态资源
if (fs.existsSync(realPath)) {
fs.readFile(realPath, 'binary', function(err, file) {
if (err) {
res.writeHead(500, {
'Powered-By': 'nodePPT',
'Content-Type': 'text/plain'
});
res.end(err);
} else {
res.writeHead(200, {
'Powered-By': 'nodePPT',
'Content-Type': mimes[ext]
});
res.write(file, 'binary');
res.end();
}

});

} else {
res.writeHead(404, {
'Powered-By': 'nodePPT',
'Content-Type': mimes.txt
});
} else {
res.writeHead(404, {
'Powered-By': 'nodePPT',
'Content-Type': mimes.txt
});

res.write('This request URL ' + url + ' was not found on this server.');
res.end();
}
res.write('This request URL ' + url + ' was not found on this server.');
res.end();
}

}).listen(port);
}).listen(port);

return app;
}
2 changes: 1 addition & 1 deletion lib/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h2>参考资料</h2>
<canvas id="drawBoard" class="draw-board" width="900" height="700"></canvas>
</slides>
<div class="progress"><span id="progress"></span></div>
<script src="https://raw.github.com/ksky521/MixJS/master/lib/mix.0.3.0.min.js"></script>
<script src="http://rawgithub.com/ksky521/MixJS/master/lib/mix.0.3.0.min.js"></script>
<script>
var base = location.protocol + '//' + location.host + '/';
MixJS.config({
Expand Down
2 changes: 1 addition & 1 deletion ppts/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ <h2>参考资料</h2>
<canvas id="drawBoard" class="draw-board" width="900" height="700"></canvas>
</slides>
<div class="progress"><span id="progress"></span></div>
<script src="https://raw.github.com/ksky521/MixJS/master/lib/mix.0.3.0.min.js"></script>
<script type="text/javascript" src="http://rawgithub.com/ksky521/MixJS/master/lib/mix.0.3.0.min.js"></script>
<script>
var base = location.protocol + '//' + location.host + '/';
MixJS.config({
Expand Down

0 comments on commit 2683cae

Please sign in to comment.