Skip to content

Instantly share code, notes, and snippets.

@chimmelb
Created June 20, 2019 19:14
Show Gist options
  • Save chimmelb/b392beabe4dc35fce4912b28f6ca5c55 to your computer and use it in GitHub Desktop.
Save chimmelb/b392beabe4dc35fce4912b28f6ca5c55 to your computer and use it in GitHub Desktop.
primus ping-pong for own websocket
const pingRegex = /^primus::ping::(\d{13})$/
private onMessage = (messageEvent: MessageEvent) => {
try {
const data = JSON.parse(messageEvent.data)
if (pingRegex.test(data)) {
let pingResults = pingRegex.exec(data)
if (pingResults) {
this.pong(pingResults[1])
}
} else if (data.context && data.context === 'response') {
this.parseResponse(data)
}
} catch (error) {
console.error('Unparsable Response', error)
}
}
private pong = (time: string) => {
if (this.socket) {
this.socket.send(`"primus::pong::${time}"`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment