-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
141 lines (121 loc) · 5.34 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
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
/*
Copyright (c) 2017 Unify Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* jslint node: true */
'use strict';
const EventEmitter = require('events');
const express = require('express');
const app = express();
const circuit = require('./circuit');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const Patient = require('./patient');
const PatientState = require('./patientState');
const appointments = require('./appointments');
const port = process.env.PORT || 3000;
let patients = [];
let emitter = new EventEmitter();
app.use(express.static(__dirname + '/public'));
circuit.init(emitter);
/**
* Circuit module events
*/
emitter.on('update-operators', _ => {
io.in('operator').emit('patients', patients.map(p => {return p.info;}));
});
emitter.on('conf-started', callId => {
let patient = patients.find(p => { return p.session && p.session.callId === callId; });
if (patient) {
patient.status = PatientState.WITH_DOCTOR;
io.in('operator').emit('patients', patients.map(p => {return p.info;}));
}
});
emitter.on('conf-ended', callId => {
let patient = patients.find(p => { return p.session && p.session.callId === callId; });
if (patient) {
patient && patients.splice(patients.indexOf(patient), 1);
io.in('operator').emit('patients', patients.map(p => {return p.info;}));
}
});
/**
* Client socket.io connections
*/
io.on('connection', socket => {
let query = socket.handshake.query;
if (query.patient) {
appointments.get(query.patient)
.then(appointment => {
// Check if the patient is already in the list. Can be the case if user goes
// back from 'In Room' to home.
let patient = patients.find(p => { return p.id === appointment.id; });
patient && patients.splice(patients.indexOf(patient), 1);
patient = new Patient(socket, appointment);
// Update operators when wait time is updated
patient.onWaitUpdate = _ => socket.broadcast.to('operator').emit('patients', patients.map(p => {return p.info;}));
// Add patient to list and update patient and operator UI
patients.push(patient);
socket.emit('update', patient.info);
socket.broadcast.to('operator').emit('patients', patients.map(p => {return p.info;}));
})
.catch(err => {
console.error(err);
socket.disconnect();
});
socket.on('disconnect', _ => {
let patient = patients.find(p => { return p.socket.id === socket.id; });
if (!patient) {
console.error(`Patient disconnected, but not in the patients list.`);
return;
}
if (patient.status === PatientState.IN_ROOM) {
// Don't remove patient from list and instead have the SDK monitoring
// take over the patient.
} else {
// Notify operators
patient && patients.splice(patients.indexOf(patient), 1);
socket.broadcast.to('operator').emit('patients', patients.map(p => {return p.info;}));
}
});
} else if (query.operator) {
// Return patients to new operator and join the room
socket.emit('patients', patients.map(p => {return p.info;}));
socket.join('operator');
console.log(`Operator ${query.operator} connected.`);
socket.on('connect-to-doctor', id => {
let patient = patients.find(p => { return p.id === id; });
if (patient) {
circuit.createConversation(patient)
.then(obj => {
patient.session = obj;
patient.status = PatientState.IN_ROOM;
io.in('operator').emit('patients', patients.map(p => {return p.info;}));
patient.redirect(obj.url);
})
.catch(console.error);
}
});
socket.on('disconnect-patient', id => {
let patient = patients.find(p => { return p.id === id; });
if (patient) {
patient.socket.disconnect();
}
});
socket.on('disconnect', _ => console.log(`Operator disconnected`));
}
});
server.listen(port, _ => console.log(`Server listening at port ${port}`));