1616 border-radius : 8px ;
1717 margin-bottom : 20px ;
1818 }
19- .status {
20- padding : 10px ;
21- border-radius : 4px ;
22- margin-bottom : 10px ;
23- }
19+ .status { padding : 5px ; margin : 5px 0 ; border-radius : 3px ; }
20+ .success { background : # d4edda ; color : # 155724 ; }
21+ .error { background : # f8d7da ; color : # 721c24 ; }
2422 .connected {
2523 background : # d4edda ;
2624 color : # 155724 ;
5957 .event .app {
6058 border-left-color : # dc3545 ;
6159 }
60+ input , select , textarea , button { margin : 5px ; padding : 8px ; }
61+ .form-group { margin : 10px 0 ; }
62+ label { display : inline-block; width : 120px ; }
6263 button {
6364 background : # 007bff ;
6465 color : white;
@@ -100,19 +101,39 @@ <h2>Connection Status</h2>
100101 < strong > Last Event:</ strong > < span id ="lastEventTime "> -</ span >
101102 </ div >
102103 </ div >
104+
105+ < div class ="container ">
106+ < h2 > Messages</ h2 >
107+ < form onsubmit ="sendMessage(event) ">
108+ < div class ="form-group ">
109+ < label > Event Type:</ label >
110+ < input type ="text " id ="eventType " value ="message " placeholder ="message " required >
111+ </ div >
112+ < div class ="form-group ">
113+ < label > Event Data:</ label >
114+ < input type ="text " id ="eventData " value ="Hello SSE " required >
115+ </ div >
116+ < div class ="form-group ">
117+ < label > Target:</ label >
118+ < select id ="targetType " onchange ="updateTargetField() ">
119+ < option value ="all "> All Connections</ option >
120+ < option value ="scope "> Specific Scope</ option >
121+ < option value ="connection "> Specific Connection</ option >
122+ </ select >
123+ </ div >
124+ < div class ="form-group " id ="targetValueGroup " style ="display: none; ">
125+ < label > Target Value:</ label >
126+ < input type ="text " id ="targetValue " placeholder ="chat ">
127+ </ div >
128+ < button type ="submit "> Send Message</ button >
129+ </ form >
130+ </ div >
103131
104132 < div class ="container ">
105133 < h2 > Event Log</ h2 >
106134 < div id ="log " class ="log "> </ div >
107135 </ div >
108136
109- < div class ="container ">
110- < h2 > Messages</ h2 >
111- < div id ="messages " class ="log "> </ div >
112- < input id ="m " autocomplete ="off " />
113- < button id ="sendBtn " onclick ="sendMessage() "> Send</ button >
114- </ div >
115-
116137 < script >
117138 let eventSource = null ;
118139 let eventCount = 0 ;
@@ -181,7 +202,6 @@ <h2>Messages</h2>
181202 eventSource . onerror = function ( event ) {
182203 updateStatus ( 'disconnected' , 'Connection error' ) ;
183204 log ( 'Connection error occurred' , 'error' ) ;
184-
185205 if ( eventSource . readyState === EventSource . CLOSED ) {
186206 log ( 'Connection closed by server' , 'connection' ) ;
187207 }
@@ -200,34 +220,7 @@ <h2>Messages</h2>
200220 document . getElementById ( 'connectionId' ) . textContent = connectionId ;
201221 }
202222 } ) ;
203-
204- eventSource . addEventListener ( 'message' , function ( event ) {
205- eventCount ++ ;
206- document . getElementById ( 'eventCount' ) . textContent = eventCount ;
207- const msgsEl = document . getElementById ( 'messages' ) ;
208- const mEl = document . createElement ( 'div' ) ;
209- mEl . innerHTML = `${ event . data } ` ;
210- msgsEl . appendChild ( mEl ) ;
211- msgsEl . scrollTop = msgsEl . scrollHeight ;
212- } ) ;
213-
214- eventSource . addEventListener ( 'user.connect' , function ( event ) {
215- eventCount ++ ;
216- document . getElementById ( 'eventCount' ) . textContent = eventCount ;
217- log ( 'User connected: ' + event . data , 'user' ) ;
218- } ) ;
219-
220- eventSource . addEventListener ( 'user.disconnect' , function ( event ) {
221- eventCount ++ ;
222- document . getElementById ( 'eventCount' ) . textContent = eventCount ;
223- log ( 'User disconnected: ' + event . data , 'user' ) ;
224- } ) ;
225-
226- eventSource . addEventListener ( 'user.event' , function ( event ) {
227- eventCount ++ ;
228- document . getElementById ( 'eventCount' ) . textContent = eventCount ;
229- log ( '' + event . data , 'user' ) ;
230- } ) ;
223+
231224 }
232225
233226 function disconnect ( ) {
@@ -240,15 +233,73 @@ <h2>Messages</h2>
240233 document . getElementById ( 'connectionId' ) . textContent = '-' ;
241234 }
242235
243- function sendMessage ( ) {
244- var input = document . getElementById ( 'm' ) ;
245- var message = input . value ;
246- if ( message ) {
247- log ( 'Sending message: ' + message ) ;
248- var jsonMessage = '{"name":"chat","method":"messageTransmit","message":{"data":"' + message + '"}}' ;
249- eventSource . dispatchEvent ( new MessageEvent ( 'message' , { data : jsonMessage } ) ) ;
250- // Clear input field
251- input . value = '' ;
236+ //function sendMessage() {
237+ // var input = document.getElementById('m');
238+ // var message = input.value;
239+ // if (message) {
240+ // log('Sending message: ' + message);
241+ // var jsonMessage = '{"name":"chat","method":"messageTransmit","message":{"data":"' + message + '"}}';
242+ // eventSource.dispatchEvent(new MessageEvent('message', { data: jsonMessage }));
243+ // input.value = '';
244+ // }
245+ //}
246+
247+ async function sendMessage ( event ) {
248+ event . preventDefault ( ) ;
249+ const eventType = document . getElementById ( 'eventType' ) . value ;
250+ const eventData = document . getElementById ( 'eventData' ) . value ;
251+ const targetType = document . getElementById ( 'targetType' ) . value ;
252+ const targetValue = document . getElementById ( 'targetValue' ) . value ;
253+ const sender = 'sse01' ;
254+ // Build request payload
255+ const payload = {
256+ event : eventType ,
257+ data : eventData ,
258+ name : 'chat' ,
259+ method : 'messageTransmit' ,
260+ message : {
261+ data : eventData
262+ } ,
263+ sender : sender
264+ } ;
265+ if ( targetType === 'scope' && targetValue ) {
266+ payload . scope = targetValue ;
267+ } else if ( targetType === 'connection' && targetValue ) {
268+ payload . connectionId = targetValue ;
269+ }
270+ try {
271+ const response = await fetch ( eventUrl , {
272+ method : 'POST' ,
273+ headers : {
274+ 'Content-Type' : 'application/json' ,
275+ } ,
276+ body : JSON . stringify ( payload )
277+ } ) ;
278+ if ( response . ok ) {
279+ log ( 'Event sent successfully' , 'success' ) ;
280+ } else {
281+ log ( 'Failed to send event' , 'error' ) ;
282+ }
283+ document . getElementById ( 'eventData' ) . value = '' ;
284+ } catch ( error ) {
285+ log ( 'Error sending event: ${error.message}' , 'error' ) ;
286+ }
287+ }
288+
289+ // Update target field visibility
290+ function updateTargetField ( ) {
291+ const targetType = document . getElementById ( 'targetType' ) . value ;
292+ const targetGroup = document . getElementById ( 'targetValueGroup' ) ;
293+ const targetInput = document . getElementById ( 'targetValue' ) ;
294+ if ( targetType === 'all' ) {
295+ targetGroup . style . display = 'none' ;
296+ } else {
297+ targetGroup . style . display = 'block' ;
298+ if ( targetType === 'scope' ) {
299+ targetInput . placeholder = 'chat' ;
300+ } else if ( targetType === 'connection' ) {
301+ targetInput . placeholder = currentConnectionId || 'connection-id' ;
302+ }
252303 }
253304 }
254305
0 commit comments