forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonrpc.js
More file actions
177 lines (170 loc) · 4.77 KB
/
Copy pathjsonrpc.js
File metadata and controls
177 lines (170 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Copyright (c) 2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
///
/// Create new JsonRPC service.
///
/// Constructor:
/// Parameters:
/// - uri - string - service URI
/// - function_methods - optional array of strings - method names that return results
/// - notification_methods - optional array of strings - notification method names
///
/// Useful methods:
///
/// addRPCMethod(name,id)
/// Creates new method with named name
/// Parameters:
/// - name - new method name
/// - id - JSONRPC id. It should be null for notification methods
/// and it should be some integer or string for function methods
///
/// Each method given in the constructor would have following properties:
///
/// on_error(e) - Returned error, where e.type is one of 'transport', 'protocol', 'response' and
/// e.error is the error object.
/// on_result(r) - Returned method result, or on_result() - for notifications.
///
/// For example
///
/// var rpc = new JsonRPC('/chat',['getValue','getStatistics'],['updateValue']);
///
/// // Asynchronouse method
///
/// rpc.getValue.on_error = function(e) { alert('Error:' + e.error); }
/// rpc.getValue.on_result = function(r) { alert(r); }
///
/// rpc.getValue();
///
/// // Synchronous method
///
/// // not setting callbacks or setting on_error and on_result to null
/// // makes them synchronous rpc calls. For example;
///
/// alert(rpc.getStatistics());
/// rpc.updateValue(10);
///
///
function JsonRPC(uri,function_methods,notification_methods) {
if(!(this instanceof JsonRPC))
return new JsonRPC(uri,function_methods,notification_methods);
this.uri = uri;
if(typeof function_methods != 'undefined') {
for(var i=0;i<function_methods.length;i++)
{
this.addRPCMethod(function_methods[i],i);
}
}
if(typeof notification_methods != 'undefined') {
for(var i=0;i<notification_methods.length;i++)
{
this.addRPCMethod(notification_methods[i],null);
}
}
}
JsonRPC.prototype.getXHR = function() {
// Create new XMLHttpRequest for all browsers
var xhr;
if(typeof XMLHttpRequest != "undefined") {
xhr = new XMLHttpRequest();
}
else {
try { xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0"); }catch(e){}
try { xhr = new ActiveXObject("Msxml2.XMLHTTP.3.0"); }catch(e){}
try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
throw new Error("No XML HTTP Rewquest support");
}
return xhr;
}
JsonRPC.prototype.runXHR = function(xhr,name,id,params) {
// Perform actual XML HTTP Request
xhr.setRequestHeader("Content-Type","application/json");
var request = {'id' : id,'method' : name, 'params' : params };
var requestText = JSON.stringify(request);
xhr.send(requestText);
}
JsonRPC.prototype.syncCall = function(name,id,params) {
// Synchronous method call name = method name
// id null for notifcation something for functions
// params - array of parameters
var xhr = this.getXHR();
xhr.open("post",this.uri,false);
this.runXHR(xhr,name,id,params);
if(xhr.status!=200)
throw Error('Invalid response:' + xhr.status);
if(id!=null) {
var response = null;
try {
response = JSON.parse(xhr.responseText);
}
catch(e) {
throw Error('Invalid JSON-RPC response');
}
if(response.error != null)
throw Error(response.error);
return response.result;
}
}
JsonRPC.prototype.asyncCall = function(name,id,params,on_result,on_error) {
// Asynchronous method call name = method name
// id null for notifcation something for functions
// params - array of parameters
// on_result and on_error - the callbacks
//
var xhr = this.getXHR();
xhr.open("post",this.uri,true);
xhr.onreadystatechange = function() {
if(xhr.readyState!=4)
return;
if(xhr.status==200) {
if(id!=null) {
var response = null;
try {
response = JSON.parse(xhr.responseText);
}
catch(e) {
on_error({'type' : 'protocol', 'error' : 'invalid response'});
return;
}
if(response.error != null) {
on_error({'type': 'response', 'error' : response.error });
}
else {
on_result(response.result);
}
}
else {
on_result();
}
}
else {
on_error( { 'type' : 'transport', 'error' : xhr.status } );
}
}
this.runXHR(xhr,name,id,params);
}
///
/// Add new method, specify id = null for notification other valid id for function
///
JsonRPC.prototype.addRPCMethod = function(method,id) {
var call = function() {
var args = new Array();
for(var i=0;i<arguments.length;i++) {
args[i]=arguments[i];
}
if(call.on_result != null) {
this.asyncCall(method,id,args,call.on_result,call.on_error);
}
else
return this.syncCall(method,id,args);
};
call.on_error = function(e) {
throw Error(e.error);
}
call.on_result = null;
this[method]=call;
}