summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/third_party/websockets/example/quickstart
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/tools/third_party/websockets/example/quickstart')
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client.py18
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client_secure.py24
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.css33
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.html18
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js26
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.py49
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/localhost.pem48
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server.py20
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server_secure.py26
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.html9
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.js12
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.py19
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time_2.py28
13 files changed, 330 insertions, 0 deletions
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client.py
new file mode 100644
index 0000000000..8d588c2b0e
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+
+import asyncio
+import websockets
+
+async def hello():
+ uri = "ws://localhost:8765"
+ async with websockets.connect(uri) as websocket:
+ name = input("What's your name? ")
+
+ await websocket.send(name)
+ print(f">>> {name}")
+
+ greeting = await websocket.recv()
+ print(f"<<< {greeting}")
+
+if __name__ == "__main__":
+ asyncio.run(hello())
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client_secure.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client_secure.py
new file mode 100644
index 0000000000..f4b39f2b83
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/client_secure.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+import asyncio
+import pathlib
+import ssl
+import websockets
+
+ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+localhost_pem = pathlib.Path(__file__).with_name("localhost.pem")
+ssl_context.load_verify_locations(localhost_pem)
+
+async def hello():
+ uri = "wss://localhost:8765"
+ async with websockets.connect(uri, ssl=ssl_context) as websocket:
+ name = input("What's your name? ")
+
+ await websocket.send(name)
+ print(f">>> {name}")
+
+ greeting = await websocket.recv()
+ print(f"<<< {greeting}")
+
+if __name__ == "__main__":
+ asyncio.run(hello())
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.css b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.css
new file mode 100644
index 0000000000..e1f4b77148
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.css
@@ -0,0 +1,33 @@
+body {
+ font-family: "Courier New", sans-serif;
+ text-align: center;
+}
+.buttons {
+ font-size: 4em;
+ display: flex;
+ justify-content: center;
+}
+.button, .value {
+ line-height: 1;
+ padding: 2rem;
+ margin: 2rem;
+ border: medium solid;
+ min-height: 1em;
+ min-width: 1em;
+}
+.button {
+ cursor: pointer;
+ user-select: none;
+}
+.minus {
+ color: red;
+}
+.plus {
+ color: green;
+}
+.value {
+ min-width: 2em;
+}
+.state {
+ font-size: 2em;
+}
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.html b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.html
new file mode 100644
index 0000000000..2e3433bd21
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>WebSocket demo</title>
+ <link href="counter.css" rel="stylesheet">
+ </head>
+ <body>
+ <div class="buttons">
+ <div class="minus button">-</div>
+ <div class="value">?</div>
+ <div class="plus button">+</div>
+ </div>
+ <div class="state">
+ <span class="users">?</span> online
+ </div>
+ <script src="counter.js"></script>
+ </body>
+</html>
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js
new file mode 100644
index 0000000000..37d892a28b
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.js
@@ -0,0 +1,26 @@
+window.addEventListener("DOMContentLoaded", () => {
+ const websocket = new WebSocket("ws://localhost:6789/");
+
+ document.querySelector(".minus").addEventListener("click", () => {
+ websocket.send(JSON.stringify({ action: "minus" }));
+ });
+
+ document.querySelector(".plus").addEventListener("click", () => {
+ websocket.send(JSON.stringify({ action: "plus" }));
+ });
+
+ websocket.onmessage = ({ data }) => {
+ const event = JSON.parse(data);
+ switch (event.type) {
+ case "value":
+ document.querySelector(".value").textContent = event.value;
+ break;
+ case "users":
+ const users = `${event.count} user${event.count == 1 ? "" : "s"}`;
+ document.querySelector(".users").textContent = users;
+ break;
+ default:
+ console.error("unsupported event", event);
+ }
+ };
+});
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.py
new file mode 100644
index 0000000000..566e12965e
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/counter.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+import asyncio
+import json
+import logging
+import websockets
+
+logging.basicConfig()
+
+USERS = set()
+
+VALUE = 0
+
+def users_event():
+ return json.dumps({"type": "users", "count": len(USERS)})
+
+def value_event():
+ return json.dumps({"type": "value", "value": VALUE})
+
+async def counter(websocket):
+ global USERS, VALUE
+ try:
+ # Register user
+ USERS.add(websocket)
+ websockets.broadcast(USERS, users_event())
+ # Send current state to user
+ await websocket.send(value_event())
+ # Manage state changes
+ async for message in websocket:
+ event = json.loads(message)
+ if event["action"] == "minus":
+ VALUE -= 1
+ websockets.broadcast(USERS, value_event())
+ elif event["action"] == "plus":
+ VALUE += 1
+ websockets.broadcast(USERS, value_event())
+ else:
+ logging.error("unsupported event: %s", event)
+ finally:
+ # Unregister user
+ USERS.remove(websocket)
+ websockets.broadcast(USERS, users_event())
+
+async def main():
+ async with websockets.serve(counter, "localhost", 6789):
+ await asyncio.Future() # run forever
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/localhost.pem b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/localhost.pem
new file mode 100644
index 0000000000..f9a30ba8f6
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/localhost.pem
@@ -0,0 +1,48 @@
+-----BEGIN PRIVATE KEY-----
+MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDG8iDak4UBpurI
+TWjSfqJ0YVG/S56nhswehupCaIzu0xQ8wqPSs36h5t1jMexJPZfvwyvFjcV+hYpj
+LMM0wMJPx9oBQEe0bsmlC66e8aF0UpSQw1aVfYoxA9BejgEyrFNE7cRbQNYFEb/5
+3HfqZKdEQA2fgQSlZ0RTRmLrD+l72iO5o2xl5bttXpqYZB2XOkyO79j/xWdu9zFE
+sgZJ5ysWbqoRAGgnxjdYYr9DARd8bIE/hN3SW7mDt5v4LqCIhGn1VmrwtT3d5AuG
+QPz4YEbm0t6GOlmFjIMYH5Y7pALRVfoJKRj6DGNIR1JicL+wqLV66kcVnj8WKbla
+20i7fR7NAgMBAAECggEAG5yvgqbG5xvLqlFUIyMAWTbIqcxNEONcoUAIc38fUGZr
+gKNjKXNQOBha0dG0AdZSqCxmftzWdGEEfA9SaJf4YCpUz6ekTB60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+GhE4vJRx1LE/hsuAzw7BAdsTt9zicscNg2fxO
+3ao/eBcdaC6n9aFYdE6CADMpB1lCX2oWNVdj6IavQLu7VMc+WJ3RKncwC9th+5OP
+ISPvkVZWf25rR2STmvvb0qEm3CZjk4Xd7N+gxbKKUvzEgPjrLSWzKKJAWHjCLugI
+/kQqhpjWVlTbtKzWz5bViqCjSbrIPpU2MgG9AUV9y3iV
+-----END CERTIFICATE-----
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server.py
new file mode 100644
index 0000000000..31b1829729
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+import asyncio
+import websockets
+
+async def hello(websocket):
+ name = await websocket.recv()
+ print(f"<<< {name}")
+
+ greeting = f"Hello {name}!"
+
+ await websocket.send(greeting)
+ print(f">>> {greeting}")
+
+async def main():
+ async with websockets.serve(hello, "localhost", 8765):
+ await asyncio.Future() # run forever
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server_secure.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server_secure.py
new file mode 100644
index 0000000000..de41d30dc0
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/server_secure.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+import asyncio
+import pathlib
+import ssl
+import websockets
+
+async def hello(websocket):
+ name = await websocket.recv()
+ print(f"<<< {name}")
+
+ greeting = f"Hello {name}!"
+
+ await websocket.send(greeting)
+ print(f">>> {greeting}")
+
+ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
+localhost_pem = pathlib.Path(__file__).with_name("localhost.pem")
+ssl_context.load_cert_chain(localhost_pem)
+
+async def main():
+ async with websockets.serve(hello, "localhost", 8765, ssl=ssl_context):
+ await asyncio.Future() # run forever
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.html b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.html
new file mode 100644
index 0000000000..b1c93b141d
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>WebSocket demo</title>
+ </head>
+ <body>
+ <script src="show_time.js"></script>
+ </body>
+</html>
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.js b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.js
new file mode 100644
index 0000000000..26bed7ec9e
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.js
@@ -0,0 +1,12 @@
+window.addEventListener("DOMContentLoaded", () => {
+ const messages = document.createElement("ul");
+ document.body.appendChild(messages);
+
+ const websocket = new WebSocket("ws://localhost:5678/");
+ websocket.onmessage = ({ data }) => {
+ const message = document.createElement("li");
+ const content = document.createTextNode(data);
+ message.appendChild(content);
+ messages.appendChild(message);
+ };
+});
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.py
new file mode 100644
index 0000000000..a83078e8a9
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+
+import asyncio
+import datetime
+import random
+import websockets
+
+async def show_time(websocket):
+ while True:
+ message = datetime.datetime.utcnow().isoformat() + "Z"
+ await websocket.send(message)
+ await asyncio.sleep(random.random() * 2 + 1)
+
+async def main():
+ async with websockets.serve(show_time, "localhost", 5678):
+ await asyncio.Future() # run forever
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time_2.py b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time_2.py
new file mode 100644
index 0000000000..08e87f5931
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/quickstart/show_time_2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import asyncio
+import datetime
+import random
+import websockets
+
+CONNECTIONS = set()
+
+async def register(websocket):
+ CONNECTIONS.add(websocket)
+ try:
+ await websocket.wait_closed()
+ finally:
+ CONNECTIONS.remove(websocket)
+
+async def show_time():
+ while True:
+ message = datetime.datetime.utcnow().isoformat() + "Z"
+ websockets.broadcast(CONNECTIONS, message)
+ await asyncio.sleep(random.random() * 2 + 1)
+
+async def main():
+ async with websockets.serve(register, "localhost", 5678):
+ await show_time()
+
+if __name__ == "__main__":
+ asyncio.run(main())