-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestActionService.js
142 lines (123 loc) · 4.06 KB
/
testActionService.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
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
const express = require('express');
const fs = require('fs');
const http = require('http');
const WebSocket = require('ws');
const cors = require('cors');
const bodyParser = require('body-parser');
const rdf = require('rdf-ext');
const n3 = require('rdf-parser-n3');
const stringToStream = require('string-to-stream');
const XMLHttpRequest = require('xmlhttprequest-ssl');
const app = express();
const port = 4201;
//initialize a simple http server
const server = http.createServer(app);
let body = "";
let requestURI = "";
let response = "<http://localhost:4201/post> <http://localhost:4201/test-service-ns#message> <http://localhost:4201/test-service-ns#Received> .";
let asyncResponse = "<http://localhost:4201/post> <http://localhost:4201/test-service-ns#message> <http://localhost:4201/test-service-ns#AsynchronousResponse> .";
const wss = new WebSocket.Server({ server });
app.use(bodyParser.text({ type: 'text/plain', limit: '50mb' }));
app.use(bodyParser.text({ type: 'text/turtle', limit: '50mb' }));
app.use(bodyParser.text({ type: 'text/xml', limit: '50mb' }));
app.use(bodyParser.text({ type: 'application/json', limit: '50mb' }));
app.use(bodyParser.text({ type: 'application/sparql-results+xml', limit: '50mb' }));
app.use(bodyParser.text({ type: 'application/trig', limit: '50mb' }));
app.use(bodyParser());
app.use(function (err, req, res, next) {
console.error(err.stack);
});
app.use(cors());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/getResponse', (req, res) => {
res.send(response);
});
app.post('/response', (req, res) => {
console.log(req.body);
response = req.body;
res.send("");
});
app.get('/getAsyncResponse', (req, res) => {
res.send(asyncResponse);
});
app.post('/asycResponse', (req, res) => {
console.log(req.body);
asyncResponse = req.body;
res.send("");
});
app.post('/post', (req, res) => {
let wssMessage = {};
wssMessage.date = new Date().toUTCString();
wssMessage.headers = getHeaders(req.headers);
wssMessage.body = req.body;
console.log(wssMessage);
wss.clients.forEach(client => {
body = wssMessage;
client.send(JSON.stringify(wssMessage));
});
sendResponse(res, req.body);
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.on("error", (err) => {
console.log("Caught flash policy server socket error: ");
console.log(err.stack);
});
sendConnectMessage(ws);
});
server.listen(port, () => {
console.log(`Server started on port ${server.address().port} :)`);
});
function getHeaders(headers) {
let headersList = new Array();
for(var propt in headers){
headersList.push({"key": propt, "value": headers[propt]});
}
return headersList;
}
function sendConnectMessage(ws) {
let wssMessage = {};
wssMessage.body = "You are now connected to the TestActionService (testActionService.js)!";
const start = Date.now();
ws.send(JSON.stringify(wssMessage));
wssMessage.body = "This is the last response: .";
let now = Date.now();
while ((Date.now() - now) < 1000) { }
while ((Date.now() - start) < 5000) {
now = Date.now();
while ((Date.now() - now) < 500) {}
wssMessage.body = wssMessage.body + ".";
ws.send(JSON.stringify(wssMessage));
}
ws.send(JSON.stringify(body));
console.log("send!");
}
function sendResponse(res, msg) {
res.set('Content-Type', 'text/turtle');
res.send(response);
requestURI = "";
const parser = new n3();
const quadStream = parser.import(stringToStream(msg));
const graph = Promise.resolve(rdf.dataset().import(quadStream));
graph.then((value) => {
value.forEach((quad) => {
if (quad.predicate.value == "http://www.ajan.de/actn#asyncRequestURI") {
requestURI = quad.object.value;
console.log(requestURI);
}
});
if (requestURI != "") {
setTimeout(function () {
let xhr = new XMLHttpRequest();
xhr.open("POST", requestURI, true);
xhr.setRequestHeader("Content-Type", "text/turtle");
xhr.send(asyncResponse);
}, 2000);
}
});
}