This repository has been archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudcad.js
75 lines (64 loc) · 2.42 KB
/
cloudcad.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var express = require('express');
var path = require('path');
var cloudcad = express();
cloudcad.use(express.static(__dirname + '/app'));
cloudcad.get('/',function(req,res){
res.sendfile(path.join(__dirname,'/index.html'));
});
var dataBase;
var server = cloudcad.listen(1337, function() {
console.log('Listening on port %d', server.address().port);
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/passport_local_mongoose', function(err, db) {
dataBase = db;
if (err) {
throw err;
}
});
});
var io = require('socket.io').listen(server);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('export', function(msg) {
exportData = JSON.parse(msg);
var cursor = dataBase.collection('accounts').find({"sceneName": exportData.sceneName});
cursor.count(function (err, count) {
if (err != null || count == null || count == 0) {
dataBase.collection('accounts').insertOne(exportData, function (err, db) {
if (err) {
throw err;
}
console.log("Inserted scene " + exportData.sceneName + " for the user");
});
}
else {
dataBase.collection('testCollection').replaceOne({"sceneName": exportData.sceneName}, exportData, function (err, db) {
if (err) {
throw err;
}
console.log("Updated scene " + exportData.sceneName + " for the user");
});
}
});
});
socket.on('import', function(msg){
console.log("Scene " + msg + " Requested");
var importData = JSON.stringify({"sceneName": msg, 'saveData' : '', 'sessionData':''});
var cursor = dataBase.collection('accounts').find({"sceneName": msg});
cursor.count(function (err, count) {
if (err == null && count > 0) {
cursor.next(function (err, data) {
importData = JSON.stringify(data);
console.log('Retrieved the requested scene ' + importData.sceneName);
socket.emit('importData',importData);
});
}
else{
console.log('retrieve failed');
}
});
});
});