-
Notifications
You must be signed in to change notification settings - Fork 42
/
server.socketio.js
54 lines (47 loc) · 1.49 KB
/
server.socketio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module.exports = SocketIO
/**
* Mean Stack SocketIO Server File
* @name SocketIO
* @function
* @param {Object} opts the options passed to create the server
* @param {Function} done the callback function
* MIT Licensed
*/
var express = require('express')
var debug = require('debug')('meanstackjs:socketio')
var fs = require('fs')
var settings = require('./configs/settings.js').get()
var glob = require('glob')
function SocketIO (opts, done) {
var self = this
self.logger = require('./server/logger.js').logger
self.app = express()
if (settings.https.active) {
self.socketServer = require('https').createServer({
key: fs.readFileSync(settings.https.key),
cert: fs.readFileSync(settings.https.cert)
}, self.app)
} else {
self.socketServer = require('http').createServer(self.app)
}
self.io = require('socket.io')(self.socketServer)
self.io.on('connection', function (socket) {
var files = glob.sync('server/modules/**/*.socket.js')
files.forEach(function (socketFile) {
debug('Sockets: %s', socketFile)
require('./' + socketFile)(self.io, socket)
})
socket.on('system', function (msg) {
io.emit('system', msg)
})
})
self.app.set('port', settings.socketio.port)
self.socketServer.listen(self.app.get('port'))
debug('Socketio listening on port %d', self.app.get('port'))
self.logger.info('Socketio listening on port %d', self.app.get('port'))
done(null)
}
var run = require('./run.js')
if (!module.parent) {
run(SocketIO)
}