Vue UI + UI server
[e-mobility-charging-stations-simulator.git] / test / robohydra / plugins / wsServer / index.js
CommitLineData
8114d10e
JB
1// eslint-disable-next-line node/no-unpublished-require
2const RoboHydra = require('robohydra');
3const RoboHydraHead = RoboHydra.heads.RoboHydraHead;
4const RoboHydraWebSocketHead = RoboHydra.heads.RoboHydraWebSocketHead;
5const RoboHydraWebSocketHeadProxy = RoboHydra.heads.RoboHydraWebSocketHeadProxy;
47e22477 6
8114d10e 7// eslint-disable-next-line no-unused-vars
e7aeea18 8exports.getBodyParts = function (conf) {
47e22477
JB
9 let wsSocket;
10 return {
11 heads: [
12 new RoboHydraHead({
13 name: 'message',
14 path: '/message',
15 method: 'POST',
e7aeea18 16 handler: function (req, res) {
47e22477
JB
17 const msg = JSON.stringify(req.body);
18 if (wsSocket) {
19 wsSocket.send(msg);
20 res.send('Message sent');
21 } else {
22 res.send('Cannot send message, no opened websocket found');
23 }
e7aeea18 24 },
47e22477
JB
25 }),
26
27 new RoboHydraHead({
28 name: 'close',
29 path: '/close',
30 method: 'GET',
e7aeea18 31 handler: function (req, res) {
47e22477
JB
32 if (wsSocket) {
33 wsSocket.close();
34 res.send('Websocket closed');
35 } else {
36 res.send('Cannot close websocket, no opened websocket found');
37 }
e7aeea18 38 },
47e22477
JB
39 }),
40
41 new RoboHydraWebSocketHeadProxy({
42 name: 'proxy',
43 mountPath: '/proxy',
44 proxyTo: 'ws://server.example.com',
e7aeea18 45 preProcessor: function (data) {
32de5a57 46 console.info('From the client: ' + data);
47e22477 47 },
e7aeea18 48 postProcessor: function (data) {
32de5a57 49 console.info('From the server: ' + data);
e7aeea18 50 },
47e22477
JB
51 }),
52
53 new RoboHydraWebSocketHead({
54 name: 'WS Server',
55 path: '/.*',
e7aeea18 56 handler: function (req, socket) {
47e22477 57 wsSocket = socket;
e7aeea18
JB
58 },
59 }),
60 ],
47e22477
JB
61 };
62};