Skip to content

Instantly share code, notes, and snippets.

@kael
Forked from judell/wsclient.js
Created November 7, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kael/f2e9a3d8e542db2e17fa6c6e48a30e59 to your computer and use it in GitHub Desktop.
Save kael/f2e9a3d8e542db2e17fa6c6e48a30e59 to your computer and use it in GitHub Desktop.
minimal hypothesis websocket client for js
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