2424
2525 .container {
2626 width : 100% ;
27- max-width : 800 px ;
27+ max-width : 1000 px ;
2828 padding : 20px ;
2929 background-color : # ffffff ;
3030 box-shadow : 0 4px 10px rgba (0 , 0 , 0 , 0.1 );
@@ -174,6 +174,13 @@ <h1>Matching Service: WebSocket Test</h1>
174174 < label for ="userCount "> Number of Users:</ label >
175175 < input type ="number " id ="userCount " value ="2 " min ="1 " max ="20 " />
176176 < button onclick ="initializeUsers() "> Initialize Users</ button >
177+ < button onclick ="simulateUserRequestsWithDelay() ">
178+ Simulate User Requests
179+ </ button >
180+ </ div >
181+ < div id ="summaryContainer " style ="margin-top: 20px; margin-bottom: 20px ">
182+ < h3 > Summary of User Match Requests</ h3 >
183+ < div id ="summary "> </ div >
177184 </ div >
178185 < div id ="userContainer "> </ div >
179186 </ div >
@@ -227,17 +234,44 @@ <h1>Matching Service: WebSocket Test</h1>
227234 }
228235
229236 function openWebSocket ( userId ) {
230- if ( sockets [ `user${ userId } ` ] ) return ;
231-
237+ if ( sockets [ `user${ userId } ` ] ) return ; // Do not open if already open
232238 const socket = new WebSocket ( "ws://localhost:8081/match" ) ;
233239 sockets [ `user${ userId } ` ] = socket ;
234- clearResponse ( userId ) ;
240+ document . getElementById ( `response ${ userId } ` ) . innerText = "" ;
235241
236- socket . onopen = ( ) => handleSocketOpen ( socket , userId ) ;
237- socket . onmessage = ( event ) => handleSocketMessage ( event , userId ) ;
238- socket . onerror = ( error ) =>
239- console . log ( `WebSocket error for User ${ userId } :` , error ) ;
240- socket . onclose = ( ) => handleSocketClose ( userId ) ;
242+ socket . onopen = function ( ) {
243+ console . log ( `WebSocket for User ${ userId } is open now.` ) ;
244+ const { username, topics, difficulties } =
245+ userSettings [ `user${ userId } ` ] ;
246+
247+ socket . send (
248+ JSON . stringify ( {
249+ type : "match_request" ,
250+ username : username ,
251+ topics : topics ,
252+ difficulties : difficulties ,
253+ } )
254+ ) ;
255+
256+ document . getElementById (
257+ `status${ userId } `
258+ ) . innerText = `Status: matching in progress...` ;
259+ updateSummary ( ) ; // Refresh summary after opening
260+ } ;
261+
262+ socket . onmessage = function ( event ) {
263+ handleSocketMessage ( event , userId ) ;
264+ updateSummary ( ) ; // Refresh summary on message
265+ } ;
266+
267+ socket . onerror = function ( error ) {
268+ console . log ( `WebSocket error for User ${ userId } : ` + error ) ;
269+ } ;
270+
271+ socket . onclose = function ( ) {
272+ console . log ( `WebSocket for User ${ userId } is closed now.` ) ;
273+ sockets [ `user${ userId } ` ] = null ;
274+ } ;
241275 }
242276
243277 function closeConnection ( userId ) {
@@ -358,6 +392,8 @@ <h1>Matching Service: WebSocket Test</h1>
358392 ) } </pre>`;
359393 updateStatus ( userId , "Status: unexpected response" , "error" ) ;
360394 }
395+
396+ updateSummary ( ) ; // Refresh summary
361397 }
362398
363399 function handleSocketClose ( userId ) {
@@ -383,6 +419,96 @@ <h1>Matching Service: WebSocket Test</h1>
383419 function getRandomDifficulties ( ) {
384420 return difficultiesList . filter ( ( ) => Math . random ( ) < 0.4 ) ;
385421 }
422+
423+ let isSimulating = false ;
424+
425+ async function simulateUserRequestsWithDelay ( ) {
426+ if ( isSimulating ) {
427+ alert (
428+ "Simulation already in progress. Please wait until it finishes."
429+ ) ;
430+ return ;
431+ }
432+ isSimulating = true ;
433+
434+ const maxUserCount = parseInt (
435+ document . getElementById ( "userCount" ) . value ,
436+ 10
437+ ) ;
438+ initializeUsers ( ) ; // Clear existing users
439+ for ( let i = 1 ; i <= maxUserCount ; i ++ ) {
440+ // Initialize and open WebSocket for each user
441+ randomizeDetails ( i ) ;
442+ openWebSocket ( i ) ;
443+
444+ updateSummary ( ) ; // Update summary after each addition
445+
446+ // Wait for the delay before adding the next user
447+ await delay ( 1000 ) ; // 1 second delay between each user request
448+ }
449+
450+ isSimulating = false ;
451+ }
452+ function updateSummary ( ) {
453+ const summaryContainer = document . getElementById ( "summary" ) ;
454+ summaryContainer . innerHTML = "" ; // Clear previous summary
455+
456+ // Create table structure
457+ const summaryTable = document . createElement ( "table" ) ;
458+ summaryTable . style . width = "100%" ;
459+ summaryTable . style . borderCollapse = "collapse" ;
460+
461+ // Header row
462+ summaryTable . innerHTML = `
463+ <tr style="background-color: #f2f2f2;">
464+ <th style="padding: 8px; border: 1px solid #ddd;">Username</th>
465+ <th style="padding: 8px; border: 1px solid #ddd;">Status</th>
466+ <th style="padding: 8px; border: 1px solid #ddd;">Topics</th>
467+ <th style="padding: 8px; border: 1px solid #ddd;">Difficulty</th>
468+ </tr>
469+ ` ;
470+
471+ // Add each user's details to the table
472+ Object . keys ( userSettings ) . forEach ( ( userId , index ) => {
473+ const user = userSettings [ userId ] ;
474+ const statusElement = document . getElementById ( `status${ index + 1 } ` ) ;
475+ const statusText = statusElement
476+ ? statusElement . innerText
477+ : "unknown" ;
478+
479+ // Apply color coding to status
480+ let statusColor = "black" ;
481+ if ( statusText . includes ( "matching in progress" ) ) {
482+ statusColor = "blue" ;
483+ } else if ( statusText . includes ( "match found" ) ) {
484+ statusColor = "green" ;
485+ } else if ( statusText . includes ( "timed out" ) ) {
486+ statusColor = "red" ;
487+ }
488+
489+ // Create row for each user
490+ const row = document . createElement ( "tr" ) ;
491+ row . innerHTML = `
492+ <td style="padding: 8px; border: 1px solid #ddd;">${
493+ user . username
494+ } </td>
495+ <td style="padding: 8px; border: 1px solid #ddd; color: ${ statusColor } ;">${ statusText } </td>
496+ <td style="padding: 8px; border: 1px solid #ddd;">${ user . topics . join (
497+ ", "
498+ ) } </td>
499+ <td style="padding: 8px; border: 1px solid #ddd;">${ user . difficulties . join (
500+ ", "
501+ ) } </td>
502+ ` ;
503+ summaryTable . appendChild ( row ) ;
504+ } ) ;
505+
506+ summaryContainer . appendChild ( summaryTable ) ; // Append the summary table to the container
507+ }
508+
509+ function delay ( ms ) {
510+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
511+ }
386512 </ script >
387513 </ body >
388514</ html >
0 commit comments