-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.13 KB
/
index.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
var socketio = require('socket.io')
, http = require('http')
, Document = require('./lib/Document')
, Connection = require('./lib/Connection')
module.exports = function(server, opts) {
var server, io, opts = opts || {}
// set options
var options = {
initialContent: opts.initialContent || ''
}
for(var prop in options) if ('undefined' == typeof options[prop]) throw new Error('"'+prop+'" option is not set')
// We got a socket.io instance
if(server instanceof socketio.Manager) {
io = server
server = io.server
}
// we already got a pure http server
else if(server instanceof http.Server) {
io = socketio.listen(server)
}
else {
throw new Error('Server must be either an http.Server or an socketio.Manager')
}
var docs = {}
io.of('prism.io').on('connection', function(socket){
console.log('new connection: '+socket.id)
var conn = new Connection(socket)
conn.once('ready', function(data) {
if(!docs[data.document]) docs[data.document] = new Document(options.initialContent)
docs[data.document].addConnection(conn)
conn.emit('ready', data)
})
})
}