Created
June 20, 2019 19:14
-
-
Save chimmelb/b392beabe4dc35fce4912b28f6ca5c55 to your computer and use it in GitHub Desktop.
primus ping-pong for own websocket
This file contains 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
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