Advertisement
metalx1000

Websocket HTML Example

Dec 2nd, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5.   <h1>Socket Output</h1>
  6.     <a href="https://pastebin.com/6zNgQRV5">Server Examples</a>
  7.   <div id="output"></div>
  8.  
  9.   <script>
  10.     const ws = new WebSocket('ws://localhost:4444')
  11.     let output = document.querySelector("#output");
  12.     ws.onopen = () => {
  13.       console.log('ws opened on browser')
  14.       ws.send('hello world')
  15.     }
  16.  
  17.     ws.onmessage = (message) => {
  18.       console.log(`message received`, message.data)
  19.       output.innerHTML += `${message.data}<br>`;
  20.     }
  21.   </script>
  22.  
  23. </body>
  24.  
  25. </html>
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement