Skip to content

Instantly share code, notes, and snippets.

@CycloneRing
Created September 5, 2024 09:02
Show Gist options
  • Save CycloneRing/bd93a6d1de0ce5636218915c31b09509 to your computer and use it in GitHub Desktop.
Save CycloneRing/bd93a6d1de0ce5636218915c31b09509 to your computer and use it in GitHub Desktop.
HTML Websocket Client Test
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket Client</title>
</head>
<body>
<h1>WebSocket Client</h1>
<div id="messages"></div>
<input type="text" id="messageInput" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
<script>
const ws = new WebSocket('ws://localhost:8060');
ws.onopen = function() {
logMessage('Connected to WebSocket server');
};
ws.onmessage = function(event) {
logMessage('Received: ' + event.data);
};
ws.onclose = function() {
logMessage('Disconnected from WebSocket server');
};
function sendMessage() {
const input = document.getElementById('messageInput');
const message = input.value;
ws.send(message);
logMessage('Sent: ' + message);
input.value = '';
}
function logMessage(message) {
const messagesDiv = document.getElementById('messages');
const messageElement = document.createElement('div');
messageElement.textContent = message;
messagesDiv.appendChild(messageElement);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment