-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebstack.js
More file actions
128 lines (98 loc) · 2.82 KB
/
Webstack.js
File metadata and controls
128 lines (98 loc) · 2.82 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import express from 'express';
import gitApiIO from './gitApiIO.js';
import Redux from 'redux'
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
var base64 = require('base-64');
class Webstack {
constructor(port, appIndex,serverConf) {
serverConf.fileName = `aztec-${appIndex}.json`
this.appIndex = appIndex
this.serverConf = serverConf
//I'm not sure if this actually saves to GIT because we don't call the function.
this.port=port;
app.use("/static", express.static('./static/'));
app.use("/Twine", express.static('./Twine/'));
this.serverStore = Redux.createStore(this.reducer);
this.initIO();
new gitApiIO(serverConf).retrieveFileAPI().then((gameData) => {
let state = JSON.parse(gameData)
this.serverStore.dispatch({
type: 'UPDATE',
payload: state
})
http.listen(this.port, () => console.log(`App listening at http://localhost:${this.port}`));
}
).catch(err => {
console.log(err.message);
response.write(err.message, 'utf8', () => {
console.log(err.message);
})
})
console.log("port exists")
process
.on('SIGTERM', this.shutdown('SIGTERM'))
.on('SIGINT', this.shutdown('SIGINT'))
.on('uncaughtException', this.shutdown('uncaughtException'));
}
get() {
return {
app
}
}
shutdown(signal) {
return (err) => {
console.log('doing stuff')
this.saveJSON = new gitApiIO({content: base64.encode(JSON.stringify(this.serverStore.getState())),
fileName: `aztec-${this.appIndex}.json`,
...this.config})
this.saveJSON.uploadFileApi().then(
() => {
process.exit(err ? 1 : 0);
})
}
};
reducer(state, action) {
switch (action.type) {
case 'UPDATE':
return {
...state, ...action.payload
}
default:
return state
}
}
initIO() {
io.on("connect_error", (err) => {
console.log(`connect_error due to ${err.message}`);
});
io.on('connection', (socket) => {
let gstate = this.serverStore.getState();
// User connects
socket.once('new user', (id) => {
console.log("SERVER RECEIVES NEW USER:", id);
if (typeof gstate !== 'undefined') {
//console.log("gstate", JSON.stringify(gstate))
io.to(id).emit('new connection', gstate)
}
else {
//console.log("Retrieving state from JSONFS", database.getData())
io.to(id).emit('new connection', {})
}
})
socket.on('difference', (state) => {
// console.log(state)
delete state['userId'] // Removes userId from the global state (Prevents users overriding each other's userId variables)
this.serverStore.dispatch({
type: 'UPDATE',
payload: state
})
socket.broadcast.emit('difference', state)
})
});
}
}
export default Webstack;