-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
35 lines (31 loc) · 945 Bytes
/
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
const express = require('express')
const app = express()
const server = require('http').Server(app)
const net = require('net')
const pty = require('node-pty')
const WebSocket = require('ws')
app.use('/', express.static('.'))
const wss = new WebSocket.Server({ server })
wss.on('connection', ws => {
ws.send('Hi there, I am a WebSocket server')
let ptyProcess = pty.spawn('bash', ['--login'], {
name: 'xterm-color',
cols: 80,
rows: 24,
cwd: process.env.HOME,
env: process.env
})
ptyProcess.on('data', data => ws.send(JSON.stringify({ output: data })))
ws.on('message', message => {
console.log('received: %s', message)
m = JSON.parse(message)
if (m.input) {
ptyProcess.write(m.input)
} else if (m.resize) {
ptyProcess.resize(m.resize[0], m.resize[1])
}
})
})
server.listen(process.env.PORT || 8999, () => {
console.log(`Server started on port ${server.address().port} :)`)
})