Last active
November 7, 2021 17:07
-
-
Save judell/fc1df653f54b177068d6561dd80e6e72 to your computer and use it in GitHub Desktop.
minimal hypothesis websocket client for js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function connect() { | |
return new Promise(resolve => { | |
var ws = new WebSocket('wss://hypothes.is/ws?access_token=...') | |
function waitSocket() { | |
if (ws.readyState == 1) { | |
clearInterval(interval) | |
resolve(ws) | |
} | |
} | |
var interval = setInterval(waitSocket, 1000) | |
}) | |
} | |
function listen() { | |
connect().then(ws => { | |
console.log(ws) | |
ws.onmessage = function(e) { | |
console.log(e.data) | |
console.log('waiting') | |
} | |
ws.onerror = function(e) { | |
console.log('error', e) | |
ws.close() | |
listen() | |
} | |
ws.send(JSON.stringify({"type":"whoami","id":1})) | |
ws.send(JSON.stringify({"filter":{"match_policy":"include_any","clauses":[{"field":"/group","operator":"one_of","value":["__world__"],}],"actions":{"create":true,"update":true,"delete":true}}})) | |
}) | |
} | |
listen() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment