Repository navigation
Expand file tree
/
Copy pathoperator.html
More file actions
183 lines (161 loc) · 6.3 KB
/
Copy pathoperator.html
File metadata and controls
183 lines (161 loc) · 6.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Help — Operator Console</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="grid-bg"></div>
<div class="wrap">
<div class="nameplate">
<div>
<span class="tag">Operator Console</span>
<h1>Live Support</h1>
</div>
<div style="font-size:12px; color:var(--dim);">
<span class="status-dot" id="statusDot"></span><span id="statusLabel">Starting…</span>
</div>
</div>
<div class="panel" id="idlePanel">
<span class="panel-label">Line status</span>
<p style="font-size:13px; color:var(--paper);" id="idleMsg">Bringing the line up…</p>
<p style="font-size:12px; color:var(--dim); margin-top:8px;">Keep this tab open and this device awake to stay reachable. Customers connect to a fixed operator address — no queue server required.</p>
</div>
<div id="callPanel" style="display:none;">
<div class="panel" style="margin-bottom:16px;">
<span class="panel-label">Session info</span>
<p id="sessionMeta" style="font-size:13px; color:var(--paper);"></p>
</div>
<div class="video-stage">
<div class="video-box">
<video id="remoteVideo" autoplay playsinline></video>
<span class="caption">Customer</span>
</div>
<div class="video-box">
<video id="localVideo" autoplay playsinline muted></video>
<span class="caption">You</span>
</div>
</div>
<div class="panel">
<span class="panel-label">Text channel</span>
<div class="chat-log" id="chatLog"></div>
<div style="display:flex; gap:8px;">
<input type="text" id="chatInput" placeholder="Type a message…" style="margin-bottom:0;">
<button id="sendBtn">Send</button>
</div>
</div>
<div class="controls-row">
<button id="muteBtn" class="ghost">Mute Mic</button>
<button id="cameraBtn" class="ghost">Stop Camera</button>
<button id="endBtn" class="danger">End Session</button>
</div>
</div>
</div>
<script src="https://unpkg.com/peerjs@1.5.4/dist/peerjs.min.js"></script>
<script>
// Fixed operator ID — customers dial this exact string. Change it if you want
// a non-guessable address (this is NOT authentication, just an address).
const OPERATOR_ID = 'help-operator-live';
let peer, localStream, mediaConn, dataConn, busy = false;
const statusDot = document.getElementById('statusDot');
const statusLabel = document.getElementById('statusLabel');
function setStatus(text, mode) {
statusLabel.textContent = text;
statusDot.className = 'status-dot' + (mode ? ' ' + mode : '');
}
peer = new Peer(OPERATOR_ID);
peer.on('open', () => {
setStatus('On the line — waiting', 'live');
document.getElementById('idleMsg').textContent = 'Line is live. Waiting for a customer to call in.';
});
peer.on('error', (err) => {
setStatus('Line error', 'alert');
if (err.type === 'unavailable-id') {
document.getElementById('idleMsg').textContent =
'This operator address is already in use elsewhere (maybe another tab). Close other operator tabs/windows and reload.';
} else {
document.getElementById('idleMsg').textContent = 'Connection error: ' + err.type + '. Reload to retry.';
}
});
peer.on('call', async (incomingCall) => {
if (busy) {
incomingCall.close(); // already on a call — reject silently, customer sees "no answer"
return;
}
busy = true;
mediaConn = incomingCall;
const meta = incomingCall.metadata || {};
document.getElementById('sessionMeta').textContent =
`${meta.name || 'Unnamed'} — ${meta.note || 'No note provided'}`;
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
document.getElementById('localVideo').srcObject = localStream;
incomingCall.answer(localStream);
incomingCall.on('stream', (remoteStream) => {
document.getElementById('remoteVideo').srcObject = remoteStream;
document.getElementById('idlePanel').style.display = 'none';
document.getElementById('callPanel').style.display = 'block';
document.getElementById('chatLog').innerHTML = '';
setStatus('Connected', 'live');
});
incomingCall.on('close', cleanup);
});
peer.on('connection', (conn) => {
dataConn = conn;
conn.on('open', () => setupChat(conn));
});
function setupChat(conn) {
conn.on('data', (data) => appendChat('Customer', data, false));
document.getElementById('sendBtn').onclick = () => {
const input = document.getElementById('chatInput');
const text = input.value.trim();
if (!text || !conn.open) return;
conn.send(text);
appendChat('You', text, true);
input.value = '';
};
document.getElementById('chatInput').addEventListener('keydown', (e) => {
if (e.key === 'Enter') document.getElementById('sendBtn').click();
});
}
function appendChat(who, text, self) {
const log = document.getElementById('chatLog');
const line = document.createElement('div');
line.className = 'chat-line' + (self ? ' self' : '');
const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
line.innerHTML = `<span class="who">${who}:</span>${escapeHtml(text)}<span class="t">${time}</span>`;
log.appendChild(line);
log.scrollTop = log.scrollHeight;
}
function escapeHtml(s) {
const d = document.createElement('div');
d.textContent = s;
return d.innerHTML;
}
document.getElementById('muteBtn').onclick = (e) => {
const track = localStream.getAudioTracks()[0];
track.enabled = !track.enabled;
e.target.textContent = track.enabled ? 'Mute Mic' : 'Unmute Mic';
};
document.getElementById('cameraBtn').onclick = (e) => {
const track = localStream.getVideoTracks()[0];
track.enabled = !track.enabled;
e.target.textContent = track.enabled ? 'Stop Camera' : 'Start Camera';
};
document.getElementById('endBtn').onclick = () => {
if (mediaConn) mediaConn.close();
if (dataConn) dataConn.close();
cleanup();
};
function cleanup() {
if (localStream) localStream.getTracks().forEach(t => t.stop());
busy = false;
document.getElementById('callPanel').style.display = 'none';
document.getElementById('idlePanel').style.display = 'block';
document.getElementById('idleMsg').textContent = 'Line is live. Waiting for a customer to call in.';
setStatus('On the line — waiting', 'live');
}
</script>
</body>
</html>