-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js.orig
157 lines (131 loc) · 3.9 KB
/
server.js.orig
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict'
const debug = require('debug')('server')
const express = require('express')
const path = require('path')
// @todo: Check if we need body-parser.
const bodyParser = require('body-parser')
const http = require('http')
const socketIo = require('socket.io')
const Repl = require('./repl/Repl.js')
<<<<<<< HEAD
const port = process.env.PORT || 3000
=======
let histOutputs = '';
let lastOutput = '';
let currentPrompt = null;
const DEFAULT_LANG = 'ruby';
>>>>>>> streaming-mode-remove-buffer
const app = express()
app.use(bodyParser.text())
app.use(express.static('public'))
const server = http.Server(app)
const io = socketIo(server) // our websocket server
let histOutputs = ''
let lastOutput = ''
// *** Commented out to fix: "'timeoutId' is assigned a value but never used."
// let timeoutId = null
let currentPrompt = null
const DEFAULT_LANG = 'ruby'
// @todo: Check if this route is necessary -- is it ever used?
app.get('/:room', (req, res) => {
debug(`${req.method} ${req.url}, req.params: %o`, req.params)
if (req.params.room === 'favicon.ico') return
debug('path.join(__dirname, "./index.html") = %s', path.join(__dirname, './index.html'))
res.sendFile(path.join(__dirname, './index.html'))
})
// @todo: Check if order of \n\r matters.
const WELCOME_MSG = 'WELCOME TO SPACECRAFT!\n\r'
io.on('connection', (socket) => {
debug('io.on("connection", (socket) => {')
const emitOutput = (output) => {
histOutputs += output
lastOutput += output
io.emit('output', { output })
}
const initRepl = (language, welcome_msg = '') => {
<<<<<<< HEAD
debug(' [initRepl] lang: %s, welcome_msg: %s', language, welcome_msg)
Repl.kill()
Repl.init(language)
histOutputs = ''
=======
Repl.kill();
Repl.init(language);
histOutputs = '';
lastOutput = '';
>>>>>>> streaming-mode-remove-buffer
io.emit('langChange', {
language: Repl.language,
data: welcome_msg
})
Repl.process.on('data', emitOutput)
}
const getCurrentPrompt = () => {
<<<<<<< HEAD
return lastOutput.split('\n').pop()
=======
return lastOutput.split('\n').pop();
>>>>>>> streaming-mode-remove-buffer
}
// @todo: Check if this is necessary.
debug('`socket.emit("langChange", {` ~~> language: %s, data: %s', Repl.language || 'ruby', WELCOME_MSG)
socket.emit('langChange', {
language: Repl.language || DEFAULT_LANG,
data: WELCOME_MSG
})
socket.emit('output', { output: histOutputs })
io.of('/').clients((error, clients) => {
debug(' [io.of("/").clients(fn)] error: %s, clients: %s', error, clients)
if (clients.length === 1) {
initRepl(DEFAULT_LANG, WELCOME_MSG)
}
})
socket.on('initRepl', ({ language }) => {
debug(' ["initRepl"] { language: %s }', language)
currentPrompt = null
if (language === Repl.language) return
initRepl(language)
})
socket.on('evaluate', ({ code }) => {
debug(' ["evaluate"] { code: %s }', code)
currentPrompt = null
lastOutput = ''
Repl.write(code)
})
socket.on('lineChanged', ({ line }) => {
debug(' ["lineChanged"] { line: %s }', line)
if (!currentPrompt) currentPrompt = getCurrentPrompt()
io.emit('syncLine', { line, prompt: currentPrompt })
})
socket.on('clear', () => {
<<<<<<< HEAD
debug(' ["clear"]')
io.emit('clear')
histOutputs = ''
})
=======
io.emit('clear');
histOutputs = '';
});
>>>>>>> streaming-mode-remove-buffer
socket.on('disconnect', () => {
debug(' ["disconnect"]')
io.of('/').clients((error, clients) => {
<<<<<<< HEAD
debug(' [io of / .clients] error: %s, clients: %s', error, clients)
if (clients.length === 0) {
Repl.kill()
}
})
})
=======
if (clients.length === 0) Repl.kill();
});
});
>>>>>>> streaming-mode-remove-buffer
// Yjs Websockets Server Events
require('./yjs-ws-server.js')(io, socket)
})
server.listen(port, () => {
debug(`Listening on port: ${port}...`)
})