Skip to content

Commit

Permalink
ajax rpc adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Yankov committed Jun 14, 2012
1 parent 1f26cf6 commit ba1d6eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
5 changes: 5 additions & 0 deletions examples/server.rb
@@ -0,0 +1,5 @@
require 'sinatra'

get '/hi' do
"Hello World!"
end
18 changes: 9 additions & 9 deletions examples/test.html
@@ -1,28 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='../../js/nourl.js'></script>
<script type='text/javascript' src='../js/nourl.js'></script>
</head>

<body>

<script type="text/javascript">

var settings = {
host: "test.dev",
port: 80,
rpcUrl: "http://test.dev/nourl/rpc",
transport: "ajax",
require: ["user", "post"]
}

nourl.run(settings, function(){

user.get('john', function(result) {
console.log(result);
});
// user.get('john', function(result) {
// console.log(result);
// });

math.sum(1,2, function(result){
console.log(result);
});
// math.sum(1,2, function(result){
// console.log(result);
// });

});

Expand Down
46 changes: 39 additions & 7 deletions js/nourl.js
@@ -1,11 +1,27 @@
var nourl = {
init: function(settings, callback) {
if (settings.transport == undefined || settings.transport == 'ajax') {
nourl.Ajax.init(settings);
} else {
throw "Doesn't support " + settings.transport;
}

callback();
},

rpc_id: function() {
id = typeof(id) != "undefined" ? id : 0;
return id += 1;
},

run: function(settings, callback) {

nourl.settings = settings;
this.settings = settings;

// vertx.connect(settings.host, settings.port, function() {
// nourl.requireList(settings.require, callback)
// });
this.init(settings, function(){
nourl.requireList(settings.require, callback)
});

},

// function that creates stubs for methods of the required class
Expand Down Expand Up @@ -72,9 +88,8 @@ var nourl = {

// sends json-rpc formatted string to an eventbus through sockjs
rpc_exec: function(method_name, params, callback) {
vertx.connection.send(
vertx.eventbus_channel,
nourl.json_rpc_format(method_name, params, vertx.rpc_id()), function(message) {
nourl.send(
nourl.json_rpc_format(method_name, params, nourl.rpc_id()), function(message) {

if (message.error) throw method_name + ": " + message.error;

Expand All @@ -91,3 +106,20 @@ var nourl = {
}
}


nourl.Ajax = {
init: function(settings) {
this.rpcUrl = settings.rpcUrl;
nourl.send = this.send;
},

send: function(message, callback) {
var client = new XMLHttpRequest();
client.open('POST', nourl.Ajax.rpcUrl, false);
client.onreadystatechange = function() {
callback(client.responseText);
}

client.send(null);
}
}

0 comments on commit ba1d6eb

Please sign in to comment.