-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
80 lines (69 loc) · 2.49 KB
/
test.php
File metadata and controls
80 lines (69 loc) · 2.49 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
<?php
// Composer 없이 간단하게 dotenv 읽기
$env = [];
if (file_exists(__DIR__ . '/.env')) {
$lines = file(__DIR__ . '/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
[$key, $value] = explode('=', $line, 5);
$env[trim($key)] = trim($value);
}
}
// 기본값
$port = $env['PORT'] ?? '8080';
$host = $env['HOST'] ?? 'localhost';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket Sender</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container py-5">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
<h4 class="mb-0">Send Message to WebSocket Server</h4>
</div>
<div class="card-body">
<form id="wsForm" class="d-flex gap-2">
<input type="text" id="messageInput" class="form-control" placeholder="Enter text here" required />
<button type="submit" class="btn btn-primary">Send</button>
</form>
<hr />
<h6>Server Log:</h6>
<ul id="logList" class="list-group list-group-flush" style="max-height: 300px; overflow-y: auto;"></ul>
</div>
</div>
</div>
<script>
// PHP에서 읽은 .env 값으로 WebSocket URL 설정
const WS_URL = `ws://<?php echo $host ?>:<?php echo $port ?>/`;
const ws = new WebSocket(WS_URL);
const logList = document.getElementById('logList');
function addLog(message) {
const li = document.createElement('li');
li.className = 'list-group-item';
li.textContent = message;
logList.appendChild(li);
logList.scrollTop = logList.scrollHeight;
}
ws.onopen = () => addLog('✅ WebSocket connection opened');
ws.onclose = () => addLog('❌ WebSocket connection closed');
ws.onerror = (err) => addLog('⚠️ WebSocket error: ' + err);
ws.onmessage = (event) => addLog('📥 Server: ' + event.data);
const form = document.getElementById('wsForm');
form.addEventListener('submit', (e) => {
e.preventDefault();
const input = document.getElementById('messageInput');
const message = input.value.trim();
if (message) {
ws.send(message);
addLog('📤 Sent: ' + message);
input.value = '';
}
});
</script>
</body>
</html>