feat(ws): release generic WebSocket client and server v0.31.0-preview
This commit is contained in:
16
examples/websocket/index.html
Normal file
16
examples/websocket/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>GoOmni WebSocket 示例</title>
|
||||
<style>body{font:16px system-ui;max-width:720px;margin:60px auto;padding:0 24px;color:#182b3d}button,input{font:inherit;padding:10px;margin:4px}pre{white-space:pre-wrap;background:#f2f5f8;padding:20px;min-height:180px}#state{color:#52697b}</style>
|
||||
<h1>WebSocket 回显与分组广播</h1><p>打开两个标签页,连接后发送文本或广播。</p>
|
||||
<p id="state">未连接</p><button id="connect">连接</button><button id="disconnect">断开</button>
|
||||
<p><input id="message" value="你好 WebSocket" aria-label="消息"><button id="send">发送</button><button id="broadcast">广播</button></p><pre id="log"></pre>
|
||||
<script>
|
||||
let socket; const log=document.querySelector('#log'),state=document.querySelector('#state');
|
||||
function append(text){log.textContent=(log.textContent+text+'\n').slice(-12000)}
|
||||
document.querySelector('#connect').onclick=()=>{if(socket&&socket.readyState<2)return;socket=new WebSocket((location.protocol==='https:'?'wss://':'ws://')+location.host+'/ws');socket.onopen=()=>state.textContent='已连接';socket.onmessage=e=>append('收到:'+e.data);socket.onclose=e=>state.textContent='已断开,关闭码 '+e.code;socket.onerror=()=>append('连接发生错误')};
|
||||
function send(value){if(socket?.readyState===WebSocket.OPEN){socket.send(value);append('发送:'+value)}else append('请先连接')}
|
||||
document.querySelector('#send').onclick=()=>send(document.querySelector('#message').value);
|
||||
document.querySelector('#broadcast').onclick=()=>send('broadcast');
|
||||
document.querySelector('#disconnect').onclick=()=>socket?.close(1000,'');
|
||||
</script></html>
|
||||
Reference in New Issue
Block a user