-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchannels.html
More file actions
51 lines (40 loc) · 1.96 KB
/
channels.html
File metadata and controls
51 lines (40 loc) · 1.96 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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Test</title>
</head>
<body>
<script>
// Create a new WebSocket connection to the server
// The URL specifies the protocol (ws for WebSocket) and the endpoint on the server
const ws = new WebSocket('ws://localhost:8020/ws/stock/share/');
// Event handler: triggered when the WebSocket connection is successfully opened
ws.onopen = () => console.log("Connected!");
// "Connected!" will be printed in the browser console when connection succeeds
// Event handler: triggered when a message is received from the server
ws.onmessage = (event) => {
// event.data contains the message from the server (usually a string)
// JSON.parse converts the string into a JavaScript object
console.log("Received:", JSON.parse(event.data));
};
// Event handler: triggered when the WebSocket connection is closed
ws.onclose = () => console.log("Disconnected!");
// "Disconnected!" will be printed if the connection is closed
// Example of sending data to the server
// We use setTimeout to wait 1 second before sending, ensuring the connection is open
setTimeout(() => {
// ws.send sends a message to the server
// JSON.stringify converts a JavaScript object into a string
ws.send(JSON.stringify({message: "Hello from JS"}));
}, 1000);
</script>
</body>
<h1> Hello WebSocket world </h1>
</html>
<!--
Command - WebSockets Support - Notes
`python manage.py runserver` - Not reliable - Only HTTP, dev server, which is synchronous and mainly meant for development. It does not support WebSockets properly.
`daphne core.asgi:application` - Fully supported - Needed for Django Channels or async, Daphne is an ASGI server, made to handle asynchronous protocols, including WebSockets.
- Use `runserver` only for development testing HTTP.
- Use `Daphne` (or Uvicorn/Hypercorn) for WebSocket support or async Django.
-->