replacing jquery ajax calls via websockets and wamp protocol

#1

Hi,
i’m interested to reuse javascript libraries (JeasyUI for example) written for jquery with ajax calls with the new wamp protocol over websocket.

this aproach seem to work for me:

use the MockJax plugin to override ajax calls wen apropiate

Here is a javascript side of a working test if someone interested on it:

var connection = new autobahn.Connection({

url: “ws://127.0.0.1:8080/ws”,

realm: “realm1”

});

//configure mockjax to use wamp session

// and intercept all ajax calls with url like “com.wamp.*”

$.mockjax({

url: “com.wamp.*”,

response: function(settings, done) {

var self = this;

var args=Object.keys(settings.data).map(function (key) {return settings.data[key]});

connection.session.call(settings.url,args).then(

function (res) {

if(settings.url.indexOf('.select.paged') > -1)

  self.responseText=PagedDataSet(res);

else

  self.responseText=PagedDataSet(res).rows;

//console.log("result ", rx.total);

done();

},

function (err) {

console.log("error ", err);

}

);

}

});

//the wamplet return an array of

// one row count + one row field names + multiples data rows, each row is an array

//for jeasyui we need to convert this to an object like an associative array

function PagedDataSet(data) {

var result = {rows: [],total: 0};

result.total = data.shift()[0];

//console.log(‘count’);

//console.log(count);

var keys = data.shift(),

i = 0, k = 0,

obj = null,

output = [];

for (i = 0; i < data.length; i++) {

obj = {};

for (k = 0; k < keys.length; k++) {

obj[keys[k]] = data[i][k];

}

output.push(obj);

}

result.rows=output;

return result;

}

connection.onopen = function (session, details) {

console.log(“Connected”);

//session available, then build (or enable) ajax dependent objects ( edatagrid is a Jeasyui grid, for example)

$(’#dg’).edatagrid({

//urls used with ajax calls

  url: 'com.wamp.mytable.select.paged',

  saveUrl: 'com.wamp.mytable.insert',

  updateUrl: 'com.wamp.mytable.update',

  destroyUrl: 'com.wamp.mytable.delete'

});

};

connection.onclose = function (reason, details) {

console.log("Connection lost: " + reason);

};

connection.open();

``

0 Likes