jeywin / jquery-websocket

Automatically exported from code.google.com/p/jquery-websocket
0 stars 0 forks source link

updated code for fix bind problem and catch the exception #4

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I update the code for fix bind problem and catch the exception.
Below is updated the code.
/*
 * jQuery Web Sockets Plugin v0.0.1
 * http://code.google.com/p/jquery-websocket/
 *
 * This document is licensed as free software under the terms of the
 * MIT License: http://www.opensource.org/licenses/mit-license.php
 * 
 * Copyright (c) 2010 by shootaroo (Shotaro Tsubouchi).
 */
(function($){
$.extend({
    websocketSettings: {
        open: function(){},
        close: function(){},
        message: function(){},
        options: {},
        events: {}
    },
    websocket: function(url, s) {
        var ws = WebSocket ? new WebSocket( url ) : {
            send: function(m){ return false },
            close: function(){}
        };
        ws._settings = $.extend($.websocketSettings, s);
        $(ws)
            .bind('open', $.websocketSettings.open)
            .bind('close', $.websocketSettings.close)
            .bind('message', $.websocketSettings.message)
            .bind('message', function(e){
                var m = $.evalJSON(e.originalEvent.data);
                var h = $.websocketSettings.events[m.type];
                if (h) h.call(this, m);
            });
        ws._send = ws.send;
        ws.send = function(type, data) {
            var m = {command: type};
            m = $.extend(true, m, $.extend(true, {}, $.websocketSettings.options, m));
            if (data) m['data'] = data;
            try{ 
                this._send($.toJSON(m));
                } 
            catch(ex)
            { 
                alert(ex);
                return false;   
            }
            return true;
        }
        $(window).unload(function(){ ws.close(); ws = null });
        return ws;
    }
});
})(jQuery);

Original issue reported on code.google.com by yinuo....@gmail.com on 2 Apr 2011 at 12:42

GoogleCodeExporter commented 9 years ago
No one wants the exception to be displayed through an alert box.

You may want to add try/catch when you use the high-level "send" function in 
your own code.

Original comment by puig.ced...@gmail.com on 27 Feb 2012 at 3:50