Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

basis for io connections

  • Loading branch information...
commit 57e38bd45d097d476edcba3f9ffe291f622bd425 1 parent e8af738
@dawicorti authored
View
54 app.js
@@ -1,36 +1,46 @@
+/*
+ * Module dependencies
+ */
+
var express = require('express');
+var app = express();
var http = require('http');
var sio = require('socket.io');
var expressLess = require('express-less');
var browserifyExpress = require('browserify-express');
var _ = require('underscore');
-var nconf = require('nconf');
-
-nconf.env().argv();
-nconf.file(__dirname + '/config.json');
-nconf.defaults({
- debug: false,
- port: 8000,
- host: 'localhost'
-});
+var conf = require('./lib/conf');
+var server = http.createServer(app);
+var io = require('./lib/io/server')(server);
-var app = express();
-var server = http.createServer(app)
-var io = sio.listen(server, { log: nconf.get('debug') });
-app.use('/assets', express.static(__dirname + '/lib/asset'));
-app.use('/components', express.static(__dirname + '/bower_components'));
-app.use(express.bodyParser());
+/*
+ * Specialize express
+ */
+
app.set('views', __dirname + '/lib/page');
app.set('view engine', 'jade');
-app.use('/less', expressLess(__dirname + '/lib/style', {compress: !nconf.get('debug')}));
+
+
+/*
+ * Register middlewares
+ */
+
+app.use('/assets', express.static(__dirname + '/lib/asset'));
+app.use('/components', express.static(__dirname + '/bower_components'));
+app.use('/less', expressLess(__dirname + '/lib/style', {compress: !conf.get('debug')}));
app.use(browserifyExpress({
entry: __dirname + '/lib/boot/game.js',
watch: __dirname + '/lib',
mount: '/boot.game.js',
- minify: !nconf.get('debug')
+ minify: !conf.get('debug')
}));
+
+/*
+ * Register routes
+ */
+
app.gameRequest = function (req, res) {
res.render('game');
};
@@ -38,9 +48,15 @@ app.gameRequest = function (req, res) {
app.get('/game/:id', app.gameRequest);
app.get('/g/:id', app.gameRequest);
+/* 404 fallback */
+
app.use(function(req, res) {
res.render('404', {sentence: _.sample(proverbs)});
});
-console.log('Meishengo started on port ' + nconf.get('port'));
-server.listen(nconf.get('port'));
+/*
+ * Start server
+ */
+
+console.log('Meishengo started on port ' + conf.get('port'));
+server.listen(conf.get('port'));
View
22 lib/conf.js
@@ -0,0 +1,22 @@
+/*
+ * Module dependencies
+ */
+
+var conf = module.exports = require('nconf');
+
+
+/* Last fallback : environment variables */
+
+conf.env().argv();
+
+/* Dev fallback : config.json */
+
+conf.file(__dirname + '/../config.json');
+
+/* Default values */
+
+conf.defaults({
+ debug: false,
+ port: 8000,
+ host: 'localhost'
+});
View
13 lib/io/connection.js
@@ -0,0 +1,13 @@
+/*
+ * Module dependencies
+ */
+
+var _ = require('underscore');
+var game = require('./game');
+
+
+/* Connection callback : listen to root elements */
+
+var connection = module.exports = function (socket) {
+ socket.on('game:observe', _.bind(game, game, socket));
+};
View
12 lib/io/game.js
@@ -0,0 +1,12 @@
+/*
+ * Module dependencies
+ */
+
+var _ = require('underscore');
+
+
+/* Game callback : listen to game elements */
+
+var game = module.exports = function (socket, data) {
+ console.log('listen to game', data);
+};
View
18 lib/io/server.js
@@ -0,0 +1,18 @@
+/*
+ * Module dependencies
+ */
+
+var conf = require('../conf');
+var socketIO = require('socket.io');
+var connection = require('./connection');
+
+
+/* Wait for http server to initialize */
+
+module.exports = function (server) {
+ var io = socketIO.listen(server, { log: conf.get('debug') });
+
+ io.sockets.on('connection', connection);
+ return io;
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.