initial bootstarpping
This commit is contained in:
commit
34c9e94365
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
examples.mk
|
||||
json-rpc/
|
||||
supernode/websocket_chat
|
||||
supernode/floaddress.org.html
|
||||
supernode/Makefile
|
||||
16219
mongoose.c
Normal file
16219
mongoose.c
Normal file
File diff suppressed because it is too large
Load Diff
6641
mongoose.h
Normal file
6641
mongoose.h
Normal file
File diff suppressed because it is too large
Load Diff
85
supernode/index.html
Normal file
85
supernode/index.html
Normal file
@ -0,0 +1,85 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>WebSocket Test</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<style type="text/css">
|
||||
body {
|
||||
background-color: #789; margin: 0;
|
||||
padding: 0; font: 14px Helvetica, Arial, sans-serif;
|
||||
}
|
||||
div.content {
|
||||
width: 800px; margin: 2em auto; padding: 20px 50px;
|
||||
background-color: #fff; border-radius: 1em;
|
||||
}
|
||||
#messages {
|
||||
border: 2px solid #fec; border-radius: 1em;
|
||||
height: 10em; overflow: scroll; padding: 0.5em 1em;
|
||||
}
|
||||
a:link, a:visited { color: #69c; text-decoration: none; }
|
||||
@media (max-width: 700px) {
|
||||
body { background-color: #fff; }
|
||||
div.content {
|
||||
width: auto; margin: 0 auto; border-radius: 0;
|
||||
padding: 1em;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
var rooms = [];
|
||||
var ws = new WebSocket('ws://' + location.host);
|
||||
|
||||
if (!window.console) { window.console = { log: function() {} } };
|
||||
|
||||
ws.onopen = function(ev) { console.log(ev); };
|
||||
ws.onerror = function(ev) { console.log(ev); };
|
||||
ws.onclose = function(ev) { console.log(ev); };
|
||||
ws.onmessage = function(ev) {
|
||||
console.log(ev);
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = ev.data;
|
||||
document.getElementById('messages').appendChild(div);
|
||||
|
||||
};
|
||||
|
||||
window.onload = function() {
|
||||
document.getElementById('send_button').onclick = function(ev) {
|
||||
var msg = document.getElementById('send_input').value;
|
||||
document.getElementById('send_input').value = '';
|
||||
ws.send(msg);
|
||||
};
|
||||
document.getElementById('send_input').onkeypress = function(ev) {
|
||||
if (ev.keyCode == 13 || ev.which == 13) { //13 is enter key
|
||||
document.getElementById('send_button').click();
|
||||
}
|
||||
};
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content">
|
||||
<h1>Websocket PubSub Demonstration</h1>
|
||||
|
||||
<p>
|
||||
This page demonstrates how Mongoose could be used to implement
|
||||
<a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
|
||||
publish–subscribe pattern</a>. Open this page in several browser
|
||||
windows. Each window initiates persistent
|
||||
<a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
|
||||
connection with the server, making each browser window a websocket client.
|
||||
Send messages, and see messages sent by other clients.
|
||||
</p>
|
||||
|
||||
<div id="messages">
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<input type="text" id="send_input" />
|
||||
<button id="send_button">Send Message</button>
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
189
supernode/json-rpc.js
Normal file
189
supernode/json-rpc.js
Normal file
@ -0,0 +1,189 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Oliver Moran
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
var JSON_RPC = {};
|
||||
|
||||
|
||||
|
||||
var id = 0, callbacks = {};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Request
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Request = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
this.id = id++;
|
||||
};
|
||||
|
||||
// Implements getters and setters for the result of a JSON-RPC Request.
|
||||
// The result may be an any Object or primitive
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "result", {
|
||||
get: function () { return this._result; },
|
||||
set: function (result) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.error; // remove error state if it exists
|
||||
this._result = result;
|
||||
}
|
||||
});
|
||||
|
||||
// Implements getters and setters for the error state of a JSON-RPC Request.
|
||||
// Error should be a JSON_RPC.Error object
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "error", {
|
||||
get: function () { return this._error; },
|
||||
set: function (error) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.result; // remove result state if it exists
|
||||
this._error = error;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Request
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Request.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
id: this.id
|
||||
};
|
||||
|
||||
if (this.method !== undefined) rpc.method = this.method;
|
||||
if (this.params !== undefined) rpc.params = this.params;
|
||||
if (this.result !== undefined) rpc.result = this.result;
|
||||
if (this.error !== undefined) rpc.error = this.error;
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Notification
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Notification = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Notification
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Notification.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
method: this.method,
|
||||
params: this.params
|
||||
};
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Errror object
|
||||
* @params code A Number that indicates the error type that occurred. -32768 to -32000 are reserved.
|
||||
* @param message (optional) A String providing a short description of the error.
|
||||
* @param data (optional) A Primitive or Structured value that contains additional information about the error.
|
||||
*/
|
||||
JSON_RPC.Error = function (code, message, data) {
|
||||
this.code = code;
|
||||
if (typeof message == "string") this.message = message;
|
||||
if (data !== undefined) this.data = data;
|
||||
};
|
||||
|
||||
// stock errors
|
||||
JSON_RPC.PARSE_ERROR = new JSON_RPC.Error(-32700, "An error occurred on the server while parsing the JSON text.");
|
||||
JSON_RPC.INVALID_REQUEST = new JSON_RPC.Error(-32600, "The JSON sent is not a valid Request object.");
|
||||
JSON_RPC.METHOD_NOT_FOUND = new JSON_RPC.Error(-32601, "The method does not exist / is not available.");
|
||||
JSON_RPC.INVALID_PARAMS = new JSON_RPC.Error(-32602, "Invalid method parameter(s).");
|
||||
JSON_RPC.INTERNAL_ERROR = new JSON_RPC.Error(-32603, "Internal JSON-RPC error.");
|
||||
|
||||
/**
|
||||
* Parses a JSON-RPC string and converts to a JSON-RPC object or an Array of such strings.
|
||||
* @params rpc A String or Array to parse to a JSON-RPC object.
|
||||
*/
|
||||
JSON_RPC.parse = function (rpc) {
|
||||
// batch?
|
||||
if (rpc.constructor === Array) {
|
||||
var arr = [];
|
||||
rpc.forEach(function (el) {
|
||||
arr.push(JSON_RPC.parse(el));
|
||||
});
|
||||
return arr;
|
||||
}
|
||||
|
||||
// parsable?
|
||||
var rpc;
|
||||
try {
|
||||
rpc = JSON.parse(rpc);
|
||||
} catch (err) {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.PARSE_ERROR;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// 2.0?
|
||||
if (rpc.jsonrpc !== "2.0") {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.INVALID_REQUEST;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// request or notification?
|
||||
var obj = (rpc.id === undefined)
|
||||
? new JSON_RPC.Notification(rpc.method, rpc.params)
|
||||
: new JSON_RPC.Request(rpc.method, rpc.params);
|
||||
// have an ID?
|
||||
if (rpc.id !== undefined) obj.id = rpc.id;
|
||||
// is it a result?
|
||||
if (rpc.result !== undefined) obj.result = rpc.result;
|
||||
// is it a error?
|
||||
if (rpc.error !== undefined) {
|
||||
obj.error = new JSON_RPC.Error(
|
||||
rpc.error.code,
|
||||
rpc.error.message,
|
||||
rpc.error.data
|
||||
);
|
||||
}
|
||||
|
||||
// parsed :-)
|
||||
return obj;
|
||||
};
|
||||
|
||||
|
||||
|
||||
7654
supernode/main.html
Normal file
7654
supernode/main.html
Normal file
File diff suppressed because it is too large
Load Diff
87
supernode/websocket_chat.c
Normal file
87
supernode/websocket_chat.c
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static sig_atomic_t s_signal_received = 0;
|
||||
static const char *s_http_port = "9000";
|
||||
static struct mg_serve_http_opts s_http_server_opts;
|
||||
|
||||
static void signal_handler(int sig_num) {
|
||||
signal(sig_num, signal_handler); // Reinstantiate signal handler
|
||||
s_signal_received = sig_num;
|
||||
}
|
||||
|
||||
static int is_websocket(const struct mg_connection *nc) {
|
||||
return nc->flags & MG_F_IS_WEBSOCKET;
|
||||
}
|
||||
|
||||
static void broadcast(struct mg_connection *nc, const struct mg_str msg) {
|
||||
struct mg_connection *c;
|
||||
char buf[500];
|
||||
char addr[32];
|
||||
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
|
||||
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
|
||||
|
||||
snprintf(buf, sizeof(buf), "%s %.*s", addr, (int) msg.len, msg.p);
|
||||
printf("%s\n", buf); /* Local echo. */
|
||||
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
|
||||
if (c == nc) continue; /* Don't send to the sender. */
|
||||
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, strlen(buf)); // A single websocket frame is sent
|
||||
}
|
||||
}
|
||||
|
||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
switch (ev) {
|
||||
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
|
||||
/* New websocket connection. Tell everybody. */
|
||||
broadcast(nc, mg_mk_str("++ joined"));
|
||||
break;
|
||||
}
|
||||
case MG_EV_WEBSOCKET_FRAME: {
|
||||
struct websocket_message *wm = (struct websocket_message *) ev_data;
|
||||
/* New websocket message. Tell everybody. */
|
||||
struct mg_str d = {(char *) wm->data, wm->size}; //This is the data received data inside *ev_data
|
||||
broadcast(nc, d); //Action when data is received
|
||||
break;
|
||||
}
|
||||
case MG_EV_HTTP_REQUEST: {
|
||||
mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts);
|
||||
break;
|
||||
}
|
||||
case MG_EV_CLOSE: {
|
||||
/* Disconnect. Tell everybody. */
|
||||
if (is_websocket(nc)) {
|
||||
broadcast(nc, mg_mk_str("-- left"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
struct mg_connection *nc;
|
||||
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||
setvbuf(stderr, NULL, _IOLBF, 0);
|
||||
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
|
||||
nc = mg_bind(&mgr, s_http_port, ev_handler); //connection mg_connection and manager mg_mgr are linked along with port and event handler
|
||||
mg_set_protocol_http_websocket(nc);
|
||||
s_http_server_opts.document_root = "."; // Serve current directory
|
||||
s_http_server_opts.enable_directory_listing = "yes";
|
||||
|
||||
printf("Started on port %s\n", s_http_port);
|
||||
while (s_signal_received == 0) {
|
||||
mg_mgr_poll(&mgr, 200);
|
||||
}
|
||||
mg_mgr_free(&mgr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
296
supernode/websocketc1.html
Normal file
296
supernode/websocketc1.html
Normal file
@ -0,0 +1,296 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8" />
|
||||
<title>LocalBitcoin++ Prototype</title>
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
|
||||
/* JSON RPC Library Starts */
|
||||
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Oliver Moran
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
var JSON_RPC = {};
|
||||
|
||||
|
||||
|
||||
var id = 0, callbacks = {};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Request
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Request = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
this.id = id++;
|
||||
};
|
||||
|
||||
// Implements getters and setters for the result of a JSON-RPC Request.
|
||||
// The result may be an any Object or primitive
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "result", {
|
||||
get: function () { return this._result; },
|
||||
set: function (result) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.error; // remove error state if it exists
|
||||
this._result = result;
|
||||
}
|
||||
});
|
||||
|
||||
// Implements getters and setters for the error state of a JSON-RPC Request.
|
||||
// Error should be a JSON_RPC.Error object
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "error", {
|
||||
get: function () { return this._error; },
|
||||
set: function (error) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.result; // remove result state if it exists
|
||||
this._error = error;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Request
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Request.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
id: this.id
|
||||
};
|
||||
|
||||
if (this.method !== undefined) rpc.method = this.method;
|
||||
if (this.params !== undefined) rpc.params = this.params;
|
||||
if (this.result !== undefined) rpc.result = this.result;
|
||||
if (this.error !== undefined) rpc.error = this.error;
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Notification
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Notification = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Notification
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Notification.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
method: this.method,
|
||||
params: this.params
|
||||
};
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Errror object
|
||||
* @params code A Number that indicates the error type that occurred. -32768 to -32000 are reserved.
|
||||
* @param message (optional) A String providing a short description of the error.
|
||||
* @param data (optional) A Primitive or Structured value that contains additional information about the error.
|
||||
*/
|
||||
JSON_RPC.Error = function (code, message, data) {
|
||||
this.code = code;
|
||||
if (typeof message == "string") this.message = message;
|
||||
if (data !== undefined) this.data = data;
|
||||
};
|
||||
|
||||
// stock errors
|
||||
JSON_RPC.PARSE_ERROR = new JSON_RPC.Error(-32700, "An error occurred on the server while parsing the JSON text.");
|
||||
JSON_RPC.INVALID_REQUEST = new JSON_RPC.Error(-32600, "The JSON sent is not a valid Request object.");
|
||||
JSON_RPC.METHOD_NOT_FOUND = new JSON_RPC.Error(-32601, "The method does not exist / is not available.");
|
||||
JSON_RPC.INVALID_PARAMS = new JSON_RPC.Error(-32602, "Invalid method parameter(s).");
|
||||
JSON_RPC.INTERNAL_ERROR = new JSON_RPC.Error(-32603, "Internal JSON-RPC error.");
|
||||
|
||||
/**
|
||||
* Parses a JSON-RPC string and converts to a JSON-RPC object or an Array of such strings.
|
||||
* @params rpc A String or Array to parse to a JSON-RPC object.
|
||||
*/
|
||||
JSON_RPC.parse = function (rpc) {
|
||||
// batch?
|
||||
if (rpc.constructor === Array) {
|
||||
var arr = [];
|
||||
rpc.forEach(function (el) {
|
||||
arr.push(JSON_RPC.parse(el));
|
||||
});
|
||||
return arr;
|
||||
}
|
||||
|
||||
// parsable?
|
||||
var rpc;
|
||||
try {
|
||||
rpc = JSON.parse(rpc);
|
||||
} catch (err) {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.PARSE_ERROR;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// 2.0?
|
||||
if (rpc.jsonrpc !== "2.0") {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.INVALID_REQUEST;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// request or notification?
|
||||
var obj = (rpc.id === undefined)
|
||||
? new JSON_RPC.Notification(rpc.method, rpc.params)
|
||||
: new JSON_RPC.Request(rpc.method, rpc.params);
|
||||
// have an ID?
|
||||
if (rpc.id !== undefined) obj.id = rpc.id;
|
||||
// is it a result?
|
||||
if (rpc.result !== undefined) obj.result = rpc.result;
|
||||
// is it a error?
|
||||
if (rpc.error !== undefined) {
|
||||
obj.error = new JSON_RPC.Error(
|
||||
rpc.error.code,
|
||||
rpc.error.message,
|
||||
rpc.error.data
|
||||
);
|
||||
}
|
||||
|
||||
// parsed :-)
|
||||
return obj;
|
||||
};
|
||||
|
||||
/* JSON RPC Library Ends */
|
||||
|
||||
|
||||
/* Custom Localbitcoin++ JSON-RPC code starts here */
|
||||
|
||||
|
||||
|
||||
// var request = new JSON_RPC.Request("SignMessage", "[1,2]");
|
||||
// var id = request.id;
|
||||
// var initialJSONSend = request.toString();
|
||||
|
||||
|
||||
|
||||
|
||||
var request = new JSON_RPC.parse('{"jsonrpc":"2.0","id":0,"method":"SignMessage","params":"[3,4]"}'); //Request is Websocket data received -- websocket.onmessage
|
||||
var methodToExecute = request.method; // if successful
|
||||
executeJSONRequest(methodToExecute);
|
||||
var initialJSONSend = request.toString(); // return to client
|
||||
|
||||
|
||||
|
||||
|
||||
function executeJSONRequest(methodToExecute){
|
||||
if(methodToExecute=="SignMessage") { console.log("SignMessage Executed") };
|
||||
}
|
||||
|
||||
// Start building all functions here
|
||||
|
||||
|
||||
|
||||
/* Custom JSON-RPC code ends */
|
||||
|
||||
|
||||
/* Websocket Code Starts here */
|
||||
|
||||
var wsUri = "ws://localhost:9000/";
|
||||
var output;
|
||||
|
||||
|
||||
|
||||
function init()
|
||||
{
|
||||
output = document.getElementById("output");
|
||||
testWebSocket();
|
||||
}
|
||||
|
||||
function testWebSocket()
|
||||
{
|
||||
websocket = new WebSocket(wsUri);
|
||||
websocket.onopen = function(evt) { onOpen(evt) };
|
||||
websocket.onclose = function(evt) { onClose(evt) };
|
||||
websocket.onmessage = function(evt) { onMessage(evt) };
|
||||
websocket.onerror = function(evt) { onError(evt) };
|
||||
}
|
||||
|
||||
function onOpen(evt)
|
||||
{
|
||||
writeToScreen("CONNECTED");
|
||||
doSend("Intial Hello Message: WebSocket rocks");
|
||||
}
|
||||
|
||||
function onClose(evt)
|
||||
{
|
||||
writeToScreen("DISCONNECTED");
|
||||
}
|
||||
|
||||
function onMessage(evt)
|
||||
{
|
||||
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
|
||||
//websocket.close();
|
||||
}
|
||||
|
||||
function onError(evt)
|
||||
{
|
||||
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
|
||||
}
|
||||
|
||||
function doSend(message)
|
||||
{
|
||||
writeToScreen("SENT: " + message);
|
||||
websocket.send(message);
|
||||
websocket.send(initialJSONSend);
|
||||
}
|
||||
|
||||
function writeToScreen(message)
|
||||
{
|
||||
var pre = document.createElement("p");
|
||||
pre.style.wordWrap = "break-word";
|
||||
pre.innerHTML = message;
|
||||
output.appendChild(pre);
|
||||
}
|
||||
|
||||
window.addEventListener("load", init, false);
|
||||
|
||||
/* Websocket Code Ends Here*/
|
||||
|
||||
</script>
|
||||
|
||||
<h2>Localbitcoin++ Prototype</h2>
|
||||
|
||||
<div id="output"></div>
|
||||
Loading…
Reference in New Issue
Block a user