File size: 988 Bytes
13563a2
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
<!doctype html><meta charset="utf-8"><title>klaw-sync test</title>
<body style="font-family:monospace;padding:16px;background:#0b0f17;color:#cde">
<h3>klaw-sync WebSocket test</h3>
<input id="u" size="60" placeholder="wss://host/ws?workspace=phoenix-default"> <button onclick="go()">connect</button>
<button onclick="pull()">pull</button> <button onclick="patch()">patch(+counter)</button>
<pre id="o"></pre>
<script>
let ws,rev=0,state={};const o=document.getElementById('o');const log=m=>o.textContent=(typeof m==='string'?m:JSON.stringify(m)).slice(0,2000)+"\n"+o.textContent;
function go(){ws=new WebSocket(document.getElementById('u').value);ws.onopen=()=>{log('open');pull()};ws.onmessage=e=>{const m=JSON.parse(e.data);if(m.op==='state'){rev=m.rev;state=m.state;}log(m)};ws.onclose=()=>log('closed')}
function pull(){ws.send(JSON.stringify({op:'pull'}))}
function patch(){state.counter=(state.counter||0)+1;ws.send(JSON.stringify({op:'patch',baseRev:rev,state}))}
</script></body>