Jump to content

Stuck on a jquery plugin [solved]


Recommended Posts

We have a small function we use in our server code to periodically send messages to a web page interface to notify the user of key steps in the process.

This is in the form of

function sendMsg(body, flag = null) {
    ws.send(JSON.stringify({ body: body, flag: flag }));
}

where body is the information and the flag is the type of message

I'm trying to make the receiving of the messages a jQuery plugin and where I'm having an issue. It does all the processing it's supposed to do but I can't get it to return data back to the web page for me to grab.

//create websocket (endpoint as argument)
(function ( $ ) {
    $.MakeSocket = function( options ) {
        settings = $.extend({
            // These are the defaults.
            endpoint: '',
            data    : '',
            success : {},
            error   : {},
            open    : {},
            exit    : {}
        }, options );
        var wsURI;
        if (window.location.protocol === "https:") {
            wsURI = "wss:";
        } else {
            wsURI = "ws:";
        }
        wsURI += "//" + window.location.host;
        wsURI += window.location.pathname + settings.endpoint;
        
        var socket = new WebSocket(wsURI);
            socket.onopen = function() {
                settings.open = {body: null, flag: 'OPEN'};
            }
            socket.onmessage = function( event ) {
                var json = JSON.parse(event.data);

                if (json.flag === 'DATA') {
                    socket.send(JSON.stringify({
                        results : settings.data,
                        flag    : 'DATA',
                    }));
                } else if (json.flag === 'COMP') {
                    console.log('<Comp>', json);
                    settings.success = json;
                    socket.close();
                } else {
                    console.log('<Info>', json);
                    settings.success = json;
                    //callback( settings );
                };
            }
            socket.onerror = function(event) {
                settings.error = {body: event, flag: 'ERR'};
            }
            socket.onclose = function() {
                console.log('Close called');
                settings.exit = {body: null, flag: 'EXIT'};
            }   
        };
    };

}( jQuery ));

What this does is creates a websocket, then processes our message flags for the webpage

And we want to be able to call the websocket and message processing like so:

$.MakeSocket({
    endpoint    : "Process", 
    data        : data,
    success     : function (wsData) {
        console.log('<wsData>', wsData);
    }
});

so that we can perform functions based on if the result is a success, what type of success, error, ect. (Just like the ajax post method for jquery)

Problem is I can't seem to get any return value from the plugin to use in the webpage. 

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to post
Share on other sites

I just realized I had accidentally left in an extra }; towards the end of my plugin. Although that still doesn't change that I can't perform if/else loops or any other code in the web page based on the success return of the plugin :(

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to post
Share on other sites

Talk about getting this in just under the wire. Right at closing time today.

I don't have the code on me, but I did manage to solve the issue :)

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...