fix: ensure log prefix helper is initialized at configuration handling
[e-mobility-charging-stations-simulator.git] / test / robohydra / plugins / wsServer / index.js
1 // eslint-disable-next-line n/no-unpublished-require
2 const RoboHydra = require('robohydra');
3 const RoboHydraHead = RoboHydra.heads.RoboHydraHead;
4 const RoboHydraWebSocketHead = RoboHydra.heads.RoboHydraWebSocketHead;
5 const RoboHydraWebSocketHeadProxy = RoboHydra.heads.RoboHydraWebSocketHeadProxy;
6
7 // eslint-disable-next-line no-unused-vars
8 exports.getBodyParts = function (conf) {
9 let wsSocket;
10 return {
11 heads: [
12 new RoboHydraHead({
13 name: 'message',
14 path: '/message',
15 method: 'POST',
16 handler: function (req, res) {
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 }
24 },
25 }),
26
27 new RoboHydraHead({
28 name: 'close',
29 path: '/close',
30 method: 'GET',
31 handler: function (req, res) {
32 if (wsSocket) {
33 wsSocket.close();
34 res.send('Websocket closed');
35 } else {
36 res.send('Cannot close websocket, no opened websocket found');
37 }
38 },
39 }),
40
41 new RoboHydraWebSocketHeadProxy({
42 name: 'proxy',
43 mountPath: '/proxy',
44 proxyTo: 'ws://server.example.com',
45 preProcessor: function (data) {
46 console.info(`From the client: ${data}`);
47 },
48 postProcessor: function (data) {
49 console.info(`From the server: ${data}`);
50 },
51 }),
52
53 new RoboHydraWebSocketHead({
54 name: 'WS Server',
55 path: '/.*',
56 handler: function (req, socket) {
57 wsSocket = socket;
58 },
59 }),
60 ],
61 };
62 };