? Editing: Post:21.body Save Delete Cancel
Content changed Sign & Publish new content

Type System

Type

Follow in NewsfeedFollowing

Latest comments:

关于Pier

on May 08, 2016 ·
4 comments

Pier

  • Distributed Porn
    每个人都可以访问和贡献

  • Like and Dislike
    每个人都可以放置自己的品味

  • BitTorrent Sync
    不一定好,只是尝试

Read more

ZeroFrame.ts

on May 31, 2015
export class ZeroFrame {
    public waiting_cb = {}
    public wrapper_nonce: string
    public target: Window
    public next_message_id: number
    constructor(public url?: string) {
        this.waiting_cb = {}
        this.wrapper_nonce = document.location.href.replace(/.*wrapper_nonce=([A-Za-z0-9]+).*/, "$1")
        this.connect()
        this.next_message_id = 1
        this.init()
    }
    public init() {
    }
    public connect() {
        let self = this
        this.target = window.parent
        window.addEventListener("message", (e: MessageEvent) => {
            let message = e.data
            let cmd = message.cmd
            if (cmd == "response") {
                if (self.waiting_cb[message.to])
                    self.waiting_cb[message.to](message.result)
                else
                    self.log("Websocket callback not found:", message)
            } else if (cmd == "wrapperReady") { // Wrapper inited later
                self.cmd("innerReady")
            } else if (cmd == "ping") {
                self.response(message.id, "pong")
            } else if (cmd == "wrapperOpenedWebsocket") {
                self.onOpenWebsocket()
            } else if (cmd == "wrapperClosedWebsocket") {
                self.onCloseWebsocket()
            } else {
                self.route(cmd, message)
            }
        }, false)
        this.cmd("innerReady")
    }

    public route(cmd, message) {
        this.log("Unknown command", message)
    }
    public response(to, result) {
        this.send({ "cmd": "response", "to": to, "result": result })
    }


    public cmd(cmd, params = {}, cb = null) {
        this.send({ "cmd": cmd, "params": params }, cb)
    }

    public send(message, cb = null) {
        message.wrapper_nonce = this.wrapper_nonce
        message.id = this.next_message_id
        this.next_message_id += 1
        this.target.postMessage(message, "*")
        if (cb) {
            this.waiting_cb[message.id] = cb
        }
    }


    public log(...args) {
        console.log("[ZeroFrame]", ...args)
    }
    public onOpenWebsocket(e?) {
        this.log("Websocket open")
    }

    public onCloseWebsocket(e?) {
        this.log("Websocket close")
    }
}
Read more
Add new post

Title

21 hours ago · 2 min read ·
3 comments
Body
Read more

Title

21 hours ago · 2 min read

0 Comments:

user_name1 day ago
Reply
Body
This page is a snapshot of ZeroNet. Start your own ZeroNet for complete experience. Learn More