1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="UTF-8 " />
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7+ < title > WebSocket test page</ title >
8+ < link rel ="icon " type ="image/png " href ="https://edgestatic.azureedge.net/welcome/static/favicon.png ">
9+
10+ < style >
11+ body {
12+ font-family : system-ui;
13+ margin : 2rem ;
14+ }
15+
16+ h1 {
17+ margin : 2rem 0 ;
18+ }
19+
20+ button {
21+ font-family : inherit;
22+ font-size : inherit;
23+ }
24+
25+ # output {
26+ padding : 0 ;
27+ margin : 2rem 0 ;
28+ border : 1px solid;
29+ border-block-end : none;
30+ border-radius : 1rem ;
31+ overflow : hidden;
32+ }
33+
34+ # output : empty {
35+ display : none;
36+ }
37+
38+ # output li {
39+ padding : 1rem ;
40+ border-block-end : 1px solid;
41+ list-style : none;
42+ overflow : hidden;
43+ text-overflow : ellipsis;
44+ }
45+
46+ # output li .odd {
47+ background : # eee ;
48+ }
49+ </ style >
50+ </ head >
51+
52+ < body >
53+ < h1 > WebSocket test page</ h1 >
54+
55+ < button id ="connect "> Connect to the server</ button >
56+ < button id ="send " hidden > Send a message to the server</ button >
57+ < ul id ="output "> </ ul >
58+
59+ < script >
60+ const connectBtn = document . getElementById ( "connect" ) ;
61+ const sendBtn = document . getElementById ( "send" ) ;
62+ const output = document . getElementById ( "output" ) ;
63+
64+ let msgIndex = 0 ;
65+ function logActivity ( message ) {
66+ const li = document . createElement ( "li" ) ;
67+ li . textContent = message ;
68+ li . classList . add ( msgIndex % 2 === 0 ? "even" : "odd" ) ;
69+ msgIndex ++ ;
70+ output . prepend ( li ) ;
71+ }
72+
73+ connectBtn . addEventListener ( "click" , ( ) => {
74+ logActivity ( "Connecting..." ) ;
75+
76+ connectBtn . disabled = true ;
77+ // TODO: change URL to match the ws server location.
78+ const socket = new WebSocket ( "ws://localhost:8080" ) ;
79+
80+ socket . addEventListener ( "open" , ( event ) => {
81+ sendBtn . hidden = false ;
82+ logActivity ( "Connected to the server" ) ;
83+ } ) ;
84+
85+ sendBtn . addEventListener ( "click" , ( ) => {
86+ const data = { } ;
87+ for ( let i = 0 ; i < 3000 ; i ++ ) {
88+ data [ "key" + i ] = "z" . repeat ( 10 ) ;
89+ }
90+ socket . send ( JSON . stringify ( data ) ) ;
91+ logActivity ( "Message sent to the server" ) ;
92+ } ) ;
93+
94+ socket . addEventListener ( "message" , ( event ) => {
95+ console . log ( "Message from server " , event . data ) ;
96+ logActivity ( "Message received from the server: " + event . data . substring ( 0 , 500 ) ) ;
97+ } ) ;
98+ } , { once : true } ) ;
99+ </ script >
100+ </ body >
101+
102+ </ html >
0 commit comments