-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket-server.js
43 lines (38 loc) · 1.28 KB
/
socket-server.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
'use strict'
var socketIO = require('socket.io');
const mongoose = require('mongoose');
var ot = require('ot');
var roomList = {};
module.exports = function (server) {
var str = `#include<iostream>
using namespace std;
int main () {
// your code here
cout << "Hello World!" << endl;
return 0;
}`;
const io = socketIO(server);
io.on('connection', (socket) => {
socket.on('joinRoom', async (data) => {
console.log("joining room");
if (!roomList[data.room]) {
var socketIOServer = new ot.EditorSocketIOServer(str, [], data.room, function (socket, cb) {
var self = this;
console.log(self.document);
Code.findByIdAndUpdate(data.room, { content: self.document }, function (err) {
if (err) return cb(false);
cb(true);
});
});
roomList[data.room] = socketIOServer;
}
roomList[data.room].addClient(socket);
roomList[data.room].setName(socket, data.username);
socket.room = data.room;
socket.join(data.room);
});
socket.on('disconnect', () => {
socket.leave(socket.room);
});
});
}