495 lines
16 KiB
HTML
495 lines
16 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Pager Socket.IO Test</title>
|
|
<script src="https://cdn.socket.io/4.8.1/socket.io.min.js"></script>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
background: white;
|
|
border-radius: 12px;
|
|
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.header {
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
padding: 30px;
|
|
text-align: center;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 28px;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.header p {
|
|
opacity: 0.9;
|
|
}
|
|
|
|
.content {
|
|
padding: 30px;
|
|
}
|
|
|
|
.section {
|
|
margin-bottom: 30px;
|
|
padding: 20px;
|
|
background: #f8f9fa;
|
|
border-radius: 8px;
|
|
border-left: 4px solid #667eea;
|
|
}
|
|
|
|
.section h2 {
|
|
color: #333;
|
|
margin-bottom: 15px;
|
|
font-size: 20px;
|
|
}
|
|
|
|
.form-group {
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
color: #555;
|
|
font-weight: 500;
|
|
}
|
|
|
|
input, select {
|
|
width: 100%;
|
|
padding: 12px;
|
|
border: 2px solid #e0e0e0;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
transition: border-color 0.3s;
|
|
}
|
|
|
|
input:focus, select:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
}
|
|
|
|
.button-group {
|
|
display: flex;
|
|
gap: 10px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
button {
|
|
padding: 12px 24px;
|
|
border: none;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
flex: 1;
|
|
min-width: 120px;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: #667eea;
|
|
color: white;
|
|
}
|
|
|
|
.btn-primary:hover {
|
|
background: #5568d3;
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
|
}
|
|
|
|
.btn-success {
|
|
background: #28a745;
|
|
color: white;
|
|
}
|
|
|
|
.btn-success:hover {
|
|
background: #218838;
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 4px 12px rgba(40, 167, 69, 0.4);
|
|
}
|
|
|
|
.btn-danger {
|
|
background: #dc3545;
|
|
color: white;
|
|
}
|
|
|
|
.btn-danger:hover {
|
|
background: #c82333;
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 4px 12px rgba(220, 53, 69, 0.4);
|
|
}
|
|
|
|
.btn-secondary {
|
|
background: #6c757d;
|
|
color: white;
|
|
}
|
|
|
|
.btn-secondary:hover {
|
|
background: #5a6268;
|
|
}
|
|
|
|
.status {
|
|
padding: 15px;
|
|
border-radius: 6px;
|
|
margin-bottom: 20px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.status.connected {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
border: 1px solid #c3e6cb;
|
|
}
|
|
|
|
.status.disconnected {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
border: 1px solid #f5c6cb;
|
|
}
|
|
|
|
.status.connecting {
|
|
background: #fff3cd;
|
|
color: #856404;
|
|
border: 1px solid #ffeaa7;
|
|
}
|
|
|
|
.events {
|
|
max-height: 400px;
|
|
overflow-y: auto;
|
|
background: #1e1e1e;
|
|
color: #d4d4d4;
|
|
padding: 15px;
|
|
border-radius: 6px;
|
|
font-family: 'Courier New', monospace;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.event-item {
|
|
margin-bottom: 10px;
|
|
padding: 10px;
|
|
background: #2d2d2d;
|
|
border-radius: 4px;
|
|
border-left: 3px solid #667eea;
|
|
}
|
|
|
|
.event-time {
|
|
color: #858585;
|
|
font-size: 11px;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.event-type {
|
|
color: #4ec9b0;
|
|
font-weight: bold;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.event-data {
|
|
color: #ce9178;
|
|
white-space: pre-wrap;
|
|
word-break: break-all;
|
|
}
|
|
|
|
.room-badge {
|
|
display: inline-block;
|
|
padding: 4px 12px;
|
|
background: #667eea;
|
|
color: white;
|
|
border-radius: 12px;
|
|
font-size: 12px;
|
|
margin-left: 10px;
|
|
}
|
|
|
|
.clear-btn {
|
|
margin-top: 10px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1>🔔 Pager Socket.IO Test Client</h1>
|
|
<p>Test real-time pager notifications</p>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<!-- Connection Section -->
|
|
<div class="section">
|
|
<h2>Connection</h2>
|
|
<div class="form-group">
|
|
<label for="serverUrl">Server URL:</label>
|
|
<input type="text" id="serverUrl" value="http://localhost:4000" placeholder="http://localhost:4000">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="authToken">Admin Token (for Restaurant rooms):</label>
|
|
<input type="text" id="authToken" placeholder="Bearer token (optional, required for restaurant rooms)">
|
|
<small style="color: #666; display: block; margin-top: 5px;">Enter JWT token for admin authentication. Required when joining restaurant rooms.</small>
|
|
</div>
|
|
<div class="button-group">
|
|
<button class="btn-primary" onclick="connect()">Connect</button>
|
|
<button class="btn-danger" onclick="disconnect()">Disconnect</button>
|
|
</div>
|
|
<div id="connectionStatus" class="status disconnected">
|
|
Status: Disconnected
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Room Management Section -->
|
|
<div class="section">
|
|
<h2>Room Management</h2>
|
|
<div class="form-group">
|
|
<label for="userType">User Type:</label>
|
|
<select id="userType" onchange="updateRoomInputs()">
|
|
<option value="restaurant">Restaurant (Admin/Staff)</option>
|
|
<option value="session">Session (Public User)</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group" id="restaurantGroup">
|
|
<label for="restId">Restaurant ID:</label>
|
|
<input type="text" id="restId" placeholder="Auto-extracted from token" disabled>
|
|
<small style="color: #666; display: block; margin-top: 5px;">Restaurant ID is automatically extracted from your admin token. No need to enter manually.</small>
|
|
</div>
|
|
<div class="form-group" id="sessionGroup" style="display: none;">
|
|
<label for="cookieId">Cookie ID:</label>
|
|
<input type="text" id="cookieId" placeholder="Enter cookie ID (from pager cookie)">
|
|
</div>
|
|
<div class="button-group">
|
|
<button class="btn-success" onclick="joinRoom()">Join Room</button>
|
|
<button class="btn-secondary" onclick="leaveRoom()">Leave Room</button>
|
|
</div>
|
|
<div id="roomStatus" style="margin-top: 15px;">
|
|
<strong>Current Room:</strong> <span id="currentRoom">None</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Events Section -->
|
|
<div class="section">
|
|
<h2>Events Log <button class="btn-secondary clear-btn" onclick="clearEvents()">Clear</button></h2>
|
|
<div id="events" class="events">
|
|
<div class="event-item">
|
|
<div class="event-time">Ready to connect...</div>
|
|
<div class="event-type">INFO</div>
|
|
<div class="event-data">Enter server URL and click Connect to start</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
let socket = null;
|
|
let currentRoom = null;
|
|
|
|
function updateRoomInputs() {
|
|
const userType = document.getElementById('userType').value;
|
|
const restaurantGroup = document.getElementById('restaurantGroup');
|
|
const sessionGroup = document.getElementById('sessionGroup');
|
|
|
|
if (userType === 'restaurant') {
|
|
restaurantGroup.style.display = 'block';
|
|
sessionGroup.style.display = 'none';
|
|
} else {
|
|
restaurantGroup.style.display = 'none';
|
|
sessionGroup.style.display = 'block';
|
|
}
|
|
}
|
|
|
|
function connect() {
|
|
const serverUrl = document.getElementById('serverUrl').value || 'http://localhost:4000';
|
|
const token = document.getElementById('authToken').value.trim();
|
|
|
|
if (socket && socket.connected) {
|
|
addEvent('WARNING', 'Already connected!');
|
|
return;
|
|
}
|
|
|
|
updateStatus('connecting', 'Connecting...');
|
|
addEvent('INFO', `Connecting to ${serverUrl}/pager...`);
|
|
|
|
const authOptions = {};
|
|
if (token) {
|
|
// Remove "Bearer " prefix if present
|
|
const cleanToken = token.startsWith('Bearer ') ? token.substring(7) : token;
|
|
authOptions.token = cleanToken;
|
|
addEvent('INFO', 'Using authentication token');
|
|
}
|
|
|
|
socket = io(`${serverUrl}/pager`, {
|
|
transports: ['websocket', 'polling'],
|
|
reconnection: true,
|
|
reconnectionDelay: 1000,
|
|
reconnectionAttempts: 5,
|
|
auth: authOptions
|
|
});
|
|
|
|
socket.on('connect', () => {
|
|
updateStatus('connected', 'Connected');
|
|
addEvent('SUCCESS', `Connected! Socket ID: ${socket.id}`);
|
|
});
|
|
|
|
socket.on('disconnect', (reason) => {
|
|
updateStatus('disconnected', 'Disconnected');
|
|
addEvent('WARNING', `Disconnected: ${reason}`);
|
|
currentRoom = null;
|
|
document.getElementById('currentRoom').textContent = 'None';
|
|
});
|
|
|
|
socket.on('connect_error', (error) => {
|
|
updateStatus('disconnected', 'Connection Failed');
|
|
addEvent('ERROR', `Connection error: ${error.message}`);
|
|
});
|
|
|
|
socket.on('joined', (data) => {
|
|
addEvent('SUCCESS', `Joined room: ${data.room}`, data);
|
|
currentRoom = data.room;
|
|
document.getElementById('currentRoom').textContent = data.room;
|
|
|
|
// If it's a restaurant room, extract and display the restId
|
|
if (data.room && data.room.startsWith('restaurant:')) {
|
|
const restId = data.room.replace('restaurant:', '');
|
|
document.getElementById('restId').value = restId;
|
|
}
|
|
});
|
|
|
|
socket.on('left', (data) => {
|
|
addEvent('INFO', `Left room: ${data.room}`, data);
|
|
if (currentRoom === data.room) {
|
|
currentRoom = null;
|
|
document.getElementById('currentRoom').textContent = 'None';
|
|
}
|
|
});
|
|
|
|
socket.on('error', (data) => {
|
|
addEvent('ERROR', data.message || 'Error occurred', data);
|
|
});
|
|
|
|
// Pager events
|
|
socket.on('pager:created', (data) => {
|
|
addEvent('PAGER_CREATED', 'New pager request created!', data);
|
|
});
|
|
|
|
socket.on('pager:status:updated', (data) => {
|
|
addEvent('PAGER_STATUS_UPDATED', 'Pager status updated!', data);
|
|
});
|
|
|
|
// Handle authentication errors
|
|
socket.on('exception', (data) => {
|
|
addEvent('ERROR', `Server error: ${data.message || JSON.stringify(data)}`, data);
|
|
});
|
|
}
|
|
|
|
function disconnect() {
|
|
if (socket) {
|
|
socket.disconnect();
|
|
socket = null;
|
|
updateStatus('disconnected', 'Disconnected');
|
|
addEvent('INFO', 'Manually disconnected');
|
|
currentRoom = null;
|
|
document.getElementById('currentRoom').textContent = 'None';
|
|
}
|
|
}
|
|
|
|
function joinRoom() {
|
|
if (!socket || !socket.connected) {
|
|
addEvent('ERROR', 'Please connect first!');
|
|
return;
|
|
}
|
|
|
|
const userType = document.getElementById('userType').value;
|
|
|
|
if (userType === 'restaurant') {
|
|
const token = document.getElementById('authToken').value.trim();
|
|
if (!token) {
|
|
addEvent('ERROR', 'Admin token is required for restaurant rooms. Please enter your JWT token in the Connection section.');
|
|
return;
|
|
}
|
|
// Restaurant ID is extracted from token automatically, no need to send it
|
|
socket.emit('join:restaurant', {});
|
|
addEvent('INFO', 'Joining restaurant room (restId will be extracted from token)...');
|
|
} else {
|
|
const cookieId = document.getElementById('cookieId').value;
|
|
if (!cookieId) {
|
|
addEvent('ERROR', 'Please enter Cookie ID');
|
|
return;
|
|
}
|
|
socket.emit('join:session', { cookieId });
|
|
addEvent('INFO', `Joining session room: cookie:${cookieId}`);
|
|
}
|
|
}
|
|
|
|
function leaveRoom() {
|
|
if (!socket || !socket.connected) {
|
|
addEvent('ERROR', 'Please connect first!');
|
|
return;
|
|
}
|
|
|
|
if (!currentRoom) {
|
|
addEvent('WARNING', 'Not in any room');
|
|
return;
|
|
}
|
|
|
|
socket.emit('leave:room', { room: currentRoom });
|
|
}
|
|
|
|
function updateStatus(status, message) {
|
|
const statusEl = document.getElementById('connectionStatus');
|
|
statusEl.className = `status ${status}`;
|
|
statusEl.textContent = `Status: ${message}`;
|
|
}
|
|
|
|
function addEvent(type, message, data = null) {
|
|
const eventsDiv = document.getElementById('events');
|
|
const eventItem = document.createElement('div');
|
|
eventItem.className = 'event-item';
|
|
|
|
const time = new Date().toLocaleTimeString();
|
|
const typeColors = {
|
|
'INFO': '#4ec9b0',
|
|
'SUCCESS': '#4ec9b0',
|
|
'WARNING': '#dcdcaa',
|
|
'ERROR': '#f48771',
|
|
'PAGER_CREATED': '#4fc1ff',
|
|
'PAGER_STATUS_UPDATED': '#4fc1ff'
|
|
};
|
|
|
|
eventItem.innerHTML = `
|
|
<div class="event-time">${time}</div>
|
|
<div class="event-type" style="color: ${typeColors[type] || '#4ec9b0'}">[${type}]</div>
|
|
<div class="event-data">${message}</div>
|
|
${data ? `<div class="event-data" style="margin-top: 8px; color: #9cdcfe;">${JSON.stringify(data, null, 2)}</div>` : ''}
|
|
`;
|
|
|
|
eventsDiv.insertBefore(eventItem, eventsDiv.firstChild);
|
|
}
|
|
|
|
function clearEvents() {
|
|
document.getElementById('events').innerHTML = '';
|
|
addEvent('INFO', 'Events log cleared');
|
|
}
|
|
|
|
// Initialize
|
|
updateRoomInputs();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|