|
1 | 1 | <!DOCTYPE html>
|
2 | 2 | <html lang="en">
|
3 |
| -<head> |
4 |
| - <meta charset="UTF-8"> |
5 |
| - <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
6 | 6 | <title>Matching service: websocket test</title>
|
7 |
| -</head> |
8 |
| -<body> |
| 7 | + </head> |
| 8 | + <body> |
9 | 9 | <p id="status">Status: no matching yet</p>
|
10 | 10 | <button onclick="openWebSocket()">Find match</button>
|
11 | 11 | <button onclick="closeConnection()">Cancel matching</button>
|
12 | 12 | <p id="response"></p>
|
13 | 13 | <script>
|
14 |
| - let socket = null; |
15 |
| - |
16 |
| - function openWebSocket() { |
17 |
| - // Connect to WebSocket server |
18 |
| - // User should be authenticated first |
19 |
| - // JWT token should be sent with the initial request, in the authorization header. |
20 |
| - // request would be checked as middleware. |
21 |
| - if (socket != null) return |
22 |
| - socket = new WebSocket("ws://localhost:8081/match"); |
23 |
| - document.getElementById("response").innerText = ""; |
| 14 | + let socket = null; |
24 | 15 |
|
25 |
| - // Log connection opening |
26 |
| - socket.onopen = function() { |
27 |
| - console.log("WebSocket is open now."); |
28 |
| - |
29 |
| - // send the match request when connection is open |
30 |
| - socket.send(JSON.stringify({ |
31 |
| - type: "match_request", |
32 |
| - topics: ["Algorithms", "Arrays"], |
33 |
| - difficulties: ["Easy", "Medium"], |
34 |
| - })) |
| 16 | + function generateRandomDetails() { |
| 17 | + const username = Math.random().toString(36).substring(2, 8); // Generates a random username |
| 18 | + const domains = ["example.com", "mail.com", "test.com"]; |
| 19 | + const domain = domains[Math.floor(Math.random() * domains.length)]; |
| 20 | + return [username, `${username}@${domain}`]; |
| 21 | + } |
35 | 22 |
|
36 |
| - document.getElementById("status").innerText = "Status: matching in progress..." |
37 |
| - }; |
38 |
| - |
39 |
| - // Log response message and display it |
40 |
| - socket.onmessage = function(event) { |
41 |
| - console.log("Message received: " + event.data); |
42 |
| - document.getElementById("response").innerText = "Received: " + event.data; |
43 |
| - document.getElementById("status").innerText = "Status: matching found/timeout" |
| 23 | + function openWebSocket() { |
| 24 | + // Connect to WebSocket server |
| 25 | + // User should be authenticated first |
| 26 | + // JWT token should be sent with the initial request, in the authorization header. |
| 27 | + // request would be checked as middleware. |
| 28 | + if (socket != null) return; |
| 29 | + socket = new WebSocket("ws://localhost:8081/match"); |
| 30 | + document.getElementById("response").innerText = ""; |
44 | 31 |
|
45 |
| - }; |
46 |
| - |
47 |
| - // Log errors |
48 |
| - socket.onerror = function(error) { |
49 |
| - console.log("WebSocket error: " + error); |
50 |
| - }; |
51 |
| - |
52 |
| - // Log connection closure |
53 |
| - socket.onclose = function() { |
54 |
| - console.log("WebSocket is closed now."); |
55 |
| - socket = null |
56 |
| - }; |
57 |
| - } |
58 |
| - |
59 |
| - function closeConnection() { |
60 |
| - document.getElementById("status").innerText = "Status: matching cancelled" |
61 |
| - document.getElementById("response").innerText = ""; |
62 |
| - socket.close(); |
63 |
| - } |
| 32 | + // Log connection opening |
| 33 | + socket.onopen = function () { |
| 34 | + console.log("WebSocket is open now."); |
| 35 | + |
| 36 | + // Random email & username |
| 37 | + const arr = generateRandomDetails(); |
| 38 | + |
| 39 | + // send the match request when connection is open |
| 40 | + socket.send( |
| 41 | + JSON.stringify({ |
| 42 | + type: "match_request", |
| 43 | + topics: ["Algorithms", "Arrays"], |
| 44 | + difficulties: ["Easy", "Medium"], |
| 45 | + username: arr[0], |
| 46 | + email: arr[1], |
| 47 | + }) |
| 48 | + ); |
| 49 | + |
| 50 | + document.getElementById("status").innerText = |
| 51 | + "Status: matching in progress..."; |
| 52 | + }; |
| 53 | + |
| 54 | + // Log response message and display it |
| 55 | + socket.onmessage = function (event) { |
| 56 | + console.log("Message received: " + event.data); |
| 57 | + document.getElementById("response").innerText = |
| 58 | + "Received: " + event.data; |
| 59 | + document.getElementById("status").innerText = |
| 60 | + "Status: matching found/timeout"; |
| 61 | + }; |
| 62 | + |
| 63 | + // Log errors |
| 64 | + socket.onerror = function (error) { |
| 65 | + console.log("WebSocket error: " + error); |
| 66 | + }; |
| 67 | + |
| 68 | + // Log connection closure |
| 69 | + socket.onclose = function () { |
| 70 | + console.log("WebSocket is closed now."); |
| 71 | + socket = null; |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + function closeConnection() { |
| 76 | + document.getElementById("status").innerText = |
| 77 | + "Status: matching cancelled"; |
| 78 | + document.getElementById("response").innerText = ""; |
| 79 | + socket.close(); |
| 80 | + } |
64 | 81 | </script>
|
65 |
| -</body> |
| 82 | + </body> |
66 | 83 | </html>
|
0 commit comments