Skip to content

Instantly share code, notes, and snippets.

@judell judell/wsclient.js
Last active Dec 3, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.