ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ <!DOCTYPE html> <html> <head> <title>WebSocket Example</title> </head> <body> <h1>WebSocket Example</h1> <div id="output"></div> <script> const output = document.getElementById('output'); const ws = new WebSocket('ws://localhost:8080/websocket'); ws.onopen = (event) => { output.innerHTML += '<p>WebSocket is open.</p>'; }; ws.onmessage = (event) => { output.innerHTML += `<p>Received: ${event.data}</p>`; }; ws.onclose = (event) => { if (event.wasClean) { output.innerHTML += `<p>WebSocket closed cleanly, code=${event.code}, reason=${event.reason}</p>`; } else { output.innerHTML += '<p>Connection was abruptly closed.</p>'; } }; ws.onerror = (error) => { output.innerHTML += `<p>WebSocket error: ${error.message}</p>`; }; </script> </body> </html> ~~~