@@ -30,15 +30,15 @@ <h2>WebSocket demo</h2>
3030 < p >
3131 < label for ="location "> location:</ label >
3232 < input id ="location " type ="text " value ="ws://localhost:3000 ">
33- < button id ="connect "> connect</ button >
34- < button id ="disconnect " disabled ="disabled "> disconnect</ button >
33+ < button id ="connectBtn "> connect</ button >
34+ < button id ="disconnectBtn " disabled ="disabled "> disconnect</ button >
3535 </ p >
3636 </ fieldset >
3737 < fieldset id ="messaging " disabled ="disabled ">
3838 < p >
3939 < label for ="message "> message:</ label >
4040 < input type ="text " id ="message " value ="Hello WebSocket ">
41- < button id ="send "> send</ button >
41+ < button id ="sendBtn "> send</ button >
4242 </ p >
4343 < p >
4444 < label for ="logger "> log:</ label >
@@ -49,44 +49,61 @@ <h2>WebSocket demo</h2>
4949 < script >
5050 window . onload = function ( ) {
5151 // elements
52- var configuration = document . getElementById ( 'configuration' ) ;
53- var location = document . getElementById ( 'location' ) ;
54- var connect = document . getElementById ( 'connect ' ) ;
55- var disconnect = document . getElementById ( 'disconnect ' ) ;
56- var messaging = document . getElementById ( 'messaging' ) ;
57- var message = document . getElementById ( 'message' ) ;
58- var send = document . getElementById ( 'send ' ) ;
59- var logger = document . getElementById ( 'logger' ) ;
52+ const configuration = document . getElementById ( 'configuration' ) ;
53+ const location = document . getElementById ( 'location' ) ;
54+ const connectBtn = document . getElementById ( 'connectBtn ' ) ;
55+ const disconnectBtn = document . getElementById ( 'disconnectBtn ' ) ;
56+ const messaging = document . getElementById ( 'messaging' ) ;
57+ const message = document . getElementById ( 'message' ) ;
58+ const sendBtn = document . getElementById ( 'sendBtn ' ) ;
59+ const logger = document . getElementById ( 'logger' ) ;
6060
6161 // ws
62- var socket ;
63-
64- connect . onclick = function ( ) {
65- connect . disabled = true ;
66- disconnect . disabled = false ;
67- messaging . disabled = false ;
68-
69- socket = new WebSocket ( location . value ) ;
70- socket . onopen = function ( ) { log ( 'CONNECTED' ) ; } ;
71- socket . onclose = function ( ) { log ( 'DISCONNECTED' ) ; } ;
72- socket . onerror = function ( ) { log ( 'SOCKET ERROR OCCURED' ) ; } ;
73- socket . onmessage = function ( msg ) { log ( 'RECEIVED:' + msg . data ) ; } ;
62+ let socket ;
63+
64+ connectBtn . onclick = ( ) => { connect ( ) ; }
65+ disconnectBtn . onclick = ( ) => { disconnect ( ) ; }
66+ sendBtn . onclick = ( ) => { sendMessage ( message . value ) ; }
67+
68+ function connect ( ) {
69+ setupSocket ( location . value ) ;
7470 }
7571
76- disconnect . onclick = function ( ) {
77- connect . disabled = false ;
78- disconnect . disabled = true ;
79- messaging . disabled = true ;
80- socket . close ( ) ;
72+ function disconnect ( ) {
73+ socket . close ( ) ;
74+ socket = undefined ;
8175 }
8276
83- send . onclick = function ( ) {
84- log ( 'SEND: ' + message . value ) ;
85- socket . send ( message . value ) ;
77+ function sendMessage ( val ) {
78+ log ( 'SEND: ' + val ) ;
79+ socket . send ( val ) ;
8680 } ;
8781
82+ function setupSocket ( url ) {
83+ socket = new WebSocket ( url ) ;
84+ socket . addEventListener ( 'open' , ( ) => {
85+ log ( 'CONNECTED' ) ;
86+ toggleControls ( ) ;
87+ } )
88+ socket . addEventListener ( 'close' , ( ) => {
89+ log ( 'DISCONNECTED' ) ;
90+ toggleControls ( )
91+ } )
92+ socket . addEventListener ( 'error' , ( ) => { log ( 'SOCKET ERROR OCCURED' ) ; } )
93+ socket . addEventListener ( 'message' , ( msg ) => { log ( 'RECEIVED:' + msg . data ) ; } )
94+ }
95+
8896 function log ( message ) {
89- logger . value = logger . value + message + '\n'
97+ logger . value = logger . value + message + '\n' ;
98+ logger . scrollTop = logger . scrollHeight ; // scroll to bottom
99+ }
100+
101+ function toggleControls ( ) {
102+ [ connectBtn , disconnectBtn , messaging ] . forEach ( el => toggleEnabled ( el ) )
103+ }
104+
105+ function toggleEnabled ( el ) {
106+ el . disabled = ( el . disabled ) ? false : true ;
90107 }
91108
92109 }
0 commit comments