🧪 Add tools for testing websocket
This commit is contained in:
parent
1ab4c5984e
commit
e8c39f38cc
57
test/data/websocket/index.html
Normal file
57
test/data/websocket/index.html
Normal file
@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WebSocket Durability Test</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>WebSocket Client</h1>
|
||||
<p>
|
||||
This client will send a message every 500ms, or you can send message
|
||||
manually.
|
||||
</p>
|
||||
<div id="messages"></div>
|
||||
<input type="text" id="messageInput" placeholder="Type a message..." />
|
||||
<button onclick="sendMessage()">Send Message</button>
|
||||
|
||||
<script>
|
||||
const socket = new WebSocket("ws://localhost:8000/ws");
|
||||
|
||||
socket.onopen = function (event) {
|
||||
appendMessage("Connection opened");
|
||||
|
||||
setInterval(() => autoSendMessage(), 500)
|
||||
};
|
||||
|
||||
socket.onmessage = function (event) {
|
||||
appendMessage("Received: " + event.data);
|
||||
};
|
||||
|
||||
socket.onclose = function (event) {
|
||||
appendMessage("Connection closed");
|
||||
};
|
||||
|
||||
function sendMessage() {
|
||||
const messageInput = document.getElementById("messageInput");
|
||||
const message = messageInput.value;
|
||||
|
||||
if (message.trim() !== "") {
|
||||
socket.send(message);
|
||||
appendMessage("Sent: " + message);
|
||||
messageInput.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
function autoSendMessage() {
|
||||
const message = `[AutoSend] A new message has been sent at ${new Date().toUTCString()}`;
|
||||
socket.send(message);
|
||||
}
|
||||
|
||||
function appendMessage(message) {
|
||||
const messagesDiv = document.getElementById("messages");
|
||||
messagesDiv.innerHTML += `<p>${message}</p>`;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
29
test/data/websocket/server.py
Normal file
29
test/data/websocket/server.py
Normal file
@ -0,0 +1,29 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
async def handle_websocket(websocket, path):
|
||||
# This function will be called whenever a new WebSocket connection is established
|
||||
|
||||
# Send a welcome message to the client
|
||||
await websocket.send("Welcome to the WebSocket server!")
|
||||
|
||||
try:
|
||||
# Enter the main loop to handle incoming messages
|
||||
async for message in websocket:
|
||||
# Print the received message
|
||||
print(f"Received message: {message}")
|
||||
|
||||
# Send a response back to the client
|
||||
response = f"Server received: {message}"
|
||||
await websocket.send(response)
|
||||
except websockets.exceptions.ConnectionClosedError:
|
||||
print("Connection closed by the client.")
|
||||
|
||||
# Create the WebSocket server
|
||||
start_server = websockets.serve(handle_websocket, "localhost", 8765)
|
||||
|
||||
print("WebSocket server started at ws://localhost:8765")
|
||||
|
||||
# Run the server indefinitely
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
Loading…
Reference in New Issue
Block a user