GithubHelp home page GithubHelp logo

messenger's Issues

跨域安全性问题

@afc163

源码第39行
this.win.postMessage(data, '*');

这个'*'太危险了,甚至支持跨大域,是否能改成可配的??

基于messenger的 iframe c/s 模块

概述

messenger中parent与iframe的通信是独立的,而这个模块主要是将消息查模块按C/S方式进行封装,方便前台调用

调用方式

server端

seajs.use('./libs/server',function(server)  {
    server.init('#iframe');
});

client端

seajs.use('./libs/client',function(client) {
    client.call({'method':'get','param':'window.location.href'},function(json) {
        url = json.rs || window.location.href;
                alert(url);
    });
});

模块源码

./libs/server.js

define(function(require, exports, module) {
    var Messenger = require('arale/messenger/1.0.2/messenger');
    var $ = require('$');

    var server = {
        messenger : {},

        init : function(fid) {
            if(!$.isEmptyObject(parent.messenger)) {
                return;
            }

            if(typeof(parent) == 'string') {
                if($(fid).length <= 0) {
                    throw new Error('getElement error');
                }
            }

            server.messenger = new Messenger({
                target: fid,
                onmessage: function(json) {
                    if(typeof(json) != 'object' || !json.orderno) { //消息格式
                        return;
                    }

                    orderno = json.orderno;

                    result = {};
                    result.orderno = orderno;
                    result.param = json.param;
                    //處理消息
                    switch(json.method) {
                        case 'get':
                            eval('result.rs=' + json.param);
                            this.send(result);
                            break;

                        case 'call':
                            eval('result.rs=' + json.param + '(' + json.opts + ')');
                            this.send(result);
                            break;
                    }
                }
            });
        }
    };

    module.exports = server;
});

./libs/client.js

define(function(require, exports, module) {
    var Messenger = require('arale/messenger/1.0.2/messenger');
    var $ = require('$');

    var client = {

        //client 2 server
        call : function(opts,callback) {
            if(!opts.method) {
                throw new Error('opts method is not exists');
            }

            var i = 0;
            do
            {
                date = new Date();
                orderno = date.getTime();
            }
            while (!client.dict.hasOwnProperty(orderno) && i++<=5);

            if(client.dict.hasOwnProperty(orderno)) {
                throw new Error('iframe dict create fail');
            }

            client.dict[orderno] = callback;

            //發送消息
            if(!$.isEmptyObject(client.messenger)) {
                client.init();
            }

            opts.orderno = orderno;
            client.messenger.send(opts);

        },


        //消息初始化,在base加載
        init : function() {
            if(!$.isEmptyObject(client.messenger)) {
                return;
            }

            fid = arguments[1] || parent;


            client.messenger = new Messenger({
                target: parent,
                onmessage: function(json) {
                    if(typeof(json) != 'object' || !json.orderno) { //消息格式
                        return;
                    }

                    if(typeof(parent) == 'string') {
                        if($(fid).length <= 0) {
                            throw new Error('getElement error');
                        }
                    }

                    orderno = json.orderno;
                    callback = client.dict[orderno];

                    if(!callback) {
                        return;
                    }
                    delete client.dict[orderno];

                    callback(json);
                }
            });
        }
    };

    client.dict = new Array();      //消息字典:orderno、callback
    client.messenger = {};
    module.exports = client;
});

没有销毁方法

@afc163

如题,没有提供销毁的方法。可否考虑提供?
在某些需要大量创建一次性iframe通信的时候比较没有销毁处理比较头疼

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.