-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·107 lines (73 loc) · 2.74 KB
/
server.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
const imports = require ('./imports')
const { promisify } = require("util");
const app = imports.EXPRESS();
app.use(imports.CORS());
app.use(imports.BODYPARSER.json());
app.use(imports.BODYPARSER.urlencoded({extended: false}));
app.get('/status', (request, response) => response.json({clients: clients.length}));
const PORT = 9060;
const uuid = imports.UUID
const redis = imports.REDIS
let clients = [];
let facts = [];
app.listen(PORT, () => {
console.log(`SSE at Port ${PORT}`)
})
function panoramicSessionStreamHandler(request, response, next) {
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
};
console.log(request.params)
const streamChannel = request.params["streamChannel"]
const userId = request.params["receiverUUID"];
response.writeHead(200, headers);
const channelSuffix = "careXR_"
const subscriber = redis.createClient({
url: imports.REDIS_URL
});
subscriber.on("subscribe", function(channel, count) {
console.log("Subscribed")
});
subscriber.on("message", async function(channel, data) {
const message = JSON.parse(data)
console.log("----------------------------------------------------")
console.log("SSE")
console.log(message)
let testMessage = {
state: "streaming",
focusTarget: message["focusTarget"],
focusState: message["focusState"],
focusAlias: message["focusAlias"]
};
console.log("testMessage")
console.log(testMessage)
const testData = `data: ${JSON.stringify(testMessage)}\n\n`; // Must have the 2 \n
response.write(testData);
console.log("----------------------------------------------------")
});
subscriber.unsubscribe();
subscriber.subscribe(channelSuffix + streamChannel);
let message = {
state: "connected",
};
const data = `data: ${JSON.stringify(message)}\n\n`; // Must have the 2 \n
response.write(data);
request.on('close', () => {
console.log(`${userId} Connection closed`);
subscriber.unsubscribe();
subscriber.quit();
});
}
app.get('/vr/panoramic/session/stream/receiver/:receiverUUID/:streamChannel', panoramicSessionStreamHandler);
function sendEventsToAll(newFact) {
clients.forEach(client => client.response.write(`data: ${JSON.stringify(newFact)}\n\n`))
}
async function addFact(request, respsonse, next) {
const newFact = request.body;
facts.push(newFact);
respsonse.json(newFact)
return sendEventsToAll(newFact);
}
app.post('/fact', addFact);