-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 2.15 KB
/
index.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
// Dependencies
const express = require('express');
const socketIo = require('socket.io-client');
// Environment (should be edited to your specific case)
require('dotenv').config()
const apiProtocol = process.env.PROTOCOL == 'websecure' ? 'https' : 'http';
const webSocketProtocol = process.env.PROTOCOL == 'websecure' ? 'wss' : 'ws';
const galiaHost = process.env.GALIA_HOST || 'gbe.atoptima.com';
const galiaPort = process.env.GALIA_PORT || 443;
const myHost = process.env.MY_HOST || 'http://localhost'; // Host of this (client) application
const myPort = process.env.MY_PORT || 3000; // Port of this (client) application
const applicationId = process.env.APPLICATION_ID || 'mock-app'; // Identifier of the Galia application
// Generate a token by loging-in to Galia
const accessToken = process.env.ACCESS_TOKEN || '';
// Build application
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
/////////////// WEBHOOK BEGIN
// Endpoint to receive Galia results
app.post('/webhook/echo', (req, res) => {
console.log('Webhook: Received a new result request');
console.log(req.body);
res.sendStatus(200);
})
/////////////// WEBHOOK END
/////////////// WEBSOCKET BEGIN
// Connect to server providing accessToken
const socket = socketIo.io(
`${webSocketProtocol}://${galiaHost}:${galiaPort}`,
{
auth: {
token: accessToken
},
transports: ['websocket']
}
);
// Display message to make sure connection was successful
socket.on('connect', () => {
const userId = socket.id;
console.log(`User connected on Client with id: ${userId}`);
});
// Display the result when galia sends it through the socket
socket.on(applicationId, function(data) {
console.log('Websocket: Received a new result');
console.log(data);
});
// Display the result when galia sends it through the socket
socket.on(`${applicationId}-recompute`, function(data) {
console.log('Websocket: Received a new recompute result');
console.log(data);
});
/////////////// WEBSOCKET END
// Start application
app.listen(myPort, () => {
console.log(`Minimal Galia client app listening at ${myHost}:${myPort}`);
})