-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js.bak
123 lines (84 loc) · 2.72 KB
/
test.js.bak
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
'use strict';
//Node application
//var
//http = require( 'http' ),
//express = require( 'express' ),
//app = express(),
//url = require('url'),
//io = require('socket.io'),
//server = http.createServer( app );
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var PythonShell = require('python-shell');
http.listen(3000, function(){
console.log('Express server listening on port %d in %s mode',http.address().port, app.settings.env);
});
var sessionsConnections = {};
//Express Middleware
var
morgan = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override');
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Override'));
/*
morgan('combined',{
skip: function(req,res) {return res.statusCode < 400}
});
*/
app.get( '/', function ( request, response ) {
response.send( 'Hello World from Node with Express and nodemon!' );
});
//test.html
app.get('/test.html', function (request, response){
var options = {
root: __dirname,// + '/public/',
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
var fileName = '/test.html';
response.sendFile(fileName, options, function (err) {
if(err) {
if (err.code === "ECONNABORT" && response.statusCode == 304) {
// No problem, 304 means client cache hit, so no data sent
console.log('304 cache hit for ' + fileName);
return;
}
console.error("SendFile error:", err, " (status: " + err.status + ")");
if (err.status) {
response.status(err.status).end();
}
}
else {
console.log('Sent:', fileName);
}
});
});
//Socket.IO and python-shell
io.on('connection', function(socket){
sessionsConnections[socket.handshake.sessionID] = socket;
console.log('connected to client\n')
socket.on('text', function(msg){
var pyshell = new PythonShell('parse_pos.py');
console.log('message recieved ' + msg);
//Python NLTK
// sends the client input to the Python script via stdin
pyshell.send(msg);
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log('response from python script', message);
//send python output to client
socket.emit('response', message);
console.log('message emitted ' + message);
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err) throw err;
console.log('finished'); });
});
});
});