Last active
October 7, 2019 17:48
-
-
Save GomZik/02e85df5fb7a5eb819cd to your computer and use it in GitHub Desktop.
ZeroFrame api ES6
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
const CMD_INNER_READY = 'innerReady' | |
const CMD_RESPONSE = 'response' | |
const CMD_WRAPPER_READY = 'wrapperReady' | |
const CMD_PING = 'ping' | |
const CMD_PONG = 'pong' | |
const CMD_WRAPPER_OPENED_WEBSOCKET = 'wrapperOpenedWebsocket' | |
const CMD_WRAPPER_CLOSE_WEBSOCKET = 'wrapperClosedWebsocket' | |
class ZeroFrame { | |
constructor(url) { | |
this.url = url | |
this.waiting_cb = {} | |
this.connect() | |
this.next_message_id = 1 | |
this.init() | |
} | |
init() { | |
return this | |
} | |
connect() { | |
this.target = window.parent | |
window.addEventListener('message', e => this.onMessage(e), false) | |
this.cmd(CMD_INNER_READY) | |
} | |
onMessage(e) { | |
let message = e.data | |
let cmd = message.cmd | |
if (cmd === CMD_RESPONSE) { | |
if (this.waiting_cb[message.to] !== undefined) { | |
this.waiting_cb[message.to](message.result) | |
} | |
else { | |
this.log("Websocket callback not found:", message) | |
} | |
} else if (cmd === CMD_WRAPPER_READY) { | |
this.cmd(CMD_INNER_READY) | |
} else if (cmd === CMD_PING) { | |
this.response(message.id, CMD_PONG) | |
} else if (cmd === CMD_WRAPPER_OPENED_WEBSOCKET) { | |
this.onOpenWebsocket() | |
} else if (cmd === CMD_WRAPPER_CLOSE_WEBSOCKET) { | |
this.onCloseWebsocket() | |
} else { | |
this.route(cmd, message) | |
} | |
} | |
route(cmd, message) { | |
this.log("Unknown command", message) | |
} | |
response(to, result) { | |
this.send({ | |
cmd: CMD_RESPONSE, | |
to: to, | |
result: result | |
}) | |
} | |
cmd(cmd, params={}, cb=null) { | |
this.send({ | |
cmd: cmd, | |
params: params | |
}, cb) | |
} | |
send(message, cb=null) { | |
message.id = this.next_message_id | |
this.next_message_id++ | |
this.target.postMessage(message, '*') | |
if (cb) { | |
this.waiting_cb[message.id] = cb | |
} | |
} | |
log(...args) { | |
console.log.apply(console, ['[ZeroFrame]'].concat(args)) | |
} | |
onOpenWebsocket() { | |
this.log('Websocket open') | |
} | |
onCloseWebsocket() { | |
this.log('Websocket close') | |
} | |
} | |
export default ZeroFrame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe you need the wrapper_nonce now ?