<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Gun.js Client — BEAM Interop Test</title>
<style>
body { font-family: sans-serif; max-width: 600px; margin: 2rem auto; }
#messages { border: 1px solid #ccc; height: 300px; overflow-y: auto; padding: 1rem; margin-bottom: 1rem; }
.msg { margin-bottom: 0.5rem; }
.msg .src { color: #999; font-size: 0.8em; margin-right: 0.5rem; }
#msg { width: 70%; padding: 0.5rem; }
#send { padding: 0.5rem 1rem; }
</style>
</head>
<body>
<h1>Gun.js Client</h1>
<p id="status">Connecting...</p>
<div id="messages"></div>
<input id="msg" placeholder="Type a message..." />
<button id="send">Send</button>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script>
const params = new URLSearchParams(location.search);
const relay = params.get('relay') || 'ws://localhost:4944';
document.getElementById('status').textContent = 'Connected to ' + relay;
const gun = Gun(relay);
gun.get('chat').map().on((value, key) => {
if (typeof value !== 'string') return;
const div = document.createElement('div');
div.className = 'msg';
div.innerHTML = '<span class="src">gun</span>' + value;
document.getElementById('messages').appendChild(div);
document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
});
document.getElementById('send').onclick = () => {
const text = document.getElementById('msg').value.trim();
if (!text) return;
const ts = Date.now();
gun.get('chat').get(ts).put(text);
document.getElementById('msg').value = '';
};
document.getElementById('msg').addEventListener('keypress', (e) => {
if (e.key === 'Enter') document.getElementById('send').click();
});
</script>
</body>
</html>