-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclips_server.js
116 lines (90 loc) · 3.23 KB
/
clips_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
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
// ===================
// User config section
// ===================
const appname = 'clips';
const serverport = 3010;
const client_html = '/clips_client.html';
const app_prompt = 'CLIPS> ';
const regex_replace = /CLIPS>\s$/;
const node_modules_folder = 'node_modules';
const node_modules_url = '/static';
const welcome_message = 'Welcome to CLIPS';
const server_listen_msg = 'CLIPS Server listening on port ';
// Language specific init strings
// If you want more, add further below.
// Do not forget '\n' at the end!
const initstr1 = '(bind ?jax-mate 1)\n';
const initstr2 = '';
const initstr3 = '';
// -------------------------------------------
// Be careful when editing after this point !
// -------------------------------------------
const emit_output = 'jaxmate_output';
const socket_eval = 'jaxmate_eval';
// Spawning App
const { spawn } = require('child_process');
const repl = spawn(appname, []);
// Server
var port = serverport;
var clientHTML = client_html;
var dataID = '';
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
// Passing the http.Server instance to the listen method
var io = require('socket.io').listen(server);
var input = process.stdin.pipe(repl.stdin);
// Input init/end handling
input.on('end', () => {console.log('Exit.\n'); process.exit() });
// Initial writings (add more if necessary)
input.write(initstr1);
input.write(initstr2);
input.write(initstr3);
// REPL on data event (response from interpreter)
// rtrim prompt
// buffering
var buf = '';
repl.stdout.on('data', (data) => {
answer=data.toString();
if (answer.endsWith(app_prompt)){
answer = answer.replace(regex_replace, '');
answer = buf.concat(answer); buf = '';
} else {console.log(`** begin buffering:\n${answer}`);
console.log('** end buffering');
buf = buf.concat(answer); answer = '';
};
console.log(`Out[${dataID}]:\n${answer}`);
io.emit(emit_output, {id:dataID, data:answer});
});
// The server starts listening
server.listen(port);
console.log (welcome_message);
console.log(server_listen_msg + port.toString());
// Registering the route of your app that returns the HTML start file
app.get('/', function (req, res) {
console.log("Registering app root.");
res.sendFile(__dirname + clientHTML);
});
// Expose the node_modules folder as static resources
// (to access socket.io.js in the browser)
// maybe path.join(__dirname, 'directory')
app.use(node_modules_url, express.static(node_modules_folder));
// Handling the connection
io.on('connection', function (socket) {
//console.log(socket.handshake);
// a lot of data without .handshake
console.log("Client X connected @");
// on eval
socket.on(socket_eval, function (data) {
console.log('In['+data.id+']: ' + data.data);
// send to repl process
input.write(data.data+'\n');
// push id
dataID=data.id;
// --> client debug: data.id/data.data
// socket.emit('pure_output',
// {id:data.id, data:'pure_input:'+data.data});
});
socket.on('disconnect', function(){console.log('Client disconnecting');});
});