|
| 1 | +function RPC(host) { |
| 2 | + this._host = host; |
| 3 | + this._message_id = 1; |
| 4 | + this._pending = {}; |
| 5 | + this._topic_handler = {}; |
| 6 | + this._onopen_handler = []; |
| 7 | + this._onclose_handler = []; |
| 8 | + this.methods = {}; |
| 9 | + this.DEBUG = false; |
| 10 | + |
| 11 | + this._handle_request = function(data) { |
| 12 | + if(data.method in this.methods) { |
| 13 | + var result = this.methods[data.method](data.params); |
| 14 | + |
| 15 | + var response = { |
| 16 | + jsonrpc: '2.0', |
| 17 | + id: data.id, |
| 18 | + result: result, |
| 19 | + } |
| 20 | + |
| 21 | + this._ws.send(JSON.stringify(response)); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + this.connect = function() { |
| 26 | + this._ws = new WebSocket(this._host); |
| 27 | + this._ws._rpc = this; |
| 28 | + |
| 29 | + // onopen |
| 30 | + this._ws.onopen = function(event) { |
| 31 | + for(i in this._rpc._onopen_handler) { |
| 32 | + this._rpc._onopen_handler[i](this._rpc); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + // onclose |
| 37 | + this._ws.onclose = function(event) { |
| 38 | + for(i in this._rpc._onclose_handler) { |
| 39 | + this._rpc._onclose_handler[i](this._rpc); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + // onmessage |
| 44 | + this._ws.onmessage = function(event) { |
| 45 | + data = JSON.parse(event.data); |
| 46 | + |
| 47 | + if (this._rpc.DEBUG) { |
| 48 | + console.log('RPC <<', data); |
| 49 | + } |
| 50 | + |
| 51 | + if('method' in data) { |
| 52 | + if('id' in data && data.id != null) { // request |
| 53 | + this._rpc._handle_request(data); |
| 54 | + |
| 55 | + } else { // notification |
| 56 | + if(data.method in this._rpc._topic_handler) { |
| 57 | + this._rpc._topic_handler[data.method](data.params); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + } else { |
| 62 | + if('id' in data && data.id in this._rpc._pending) { |
| 63 | + if('error' in data) { // error |
| 64 | + this._rpc._pending[data.id].error_handler( |
| 65 | + data.error, this._rpc); |
| 66 | + |
| 67 | + } else if('result' in data) { // result |
| 68 | + this._rpc._pending[data.id].success_handler( |
| 69 | + data.result, this._rpc); |
| 70 | + } |
| 71 | + |
| 72 | + delete this._rpc._pending[data.id]; |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + }; |
| 77 | + |
| 78 | + this.disconnect = function(event) { |
| 79 | + this._ws.close(); |
| 80 | + }; |
| 81 | + |
| 82 | + this.on = function(event_name, handler) { |
| 83 | + switch(event_name) { |
| 84 | + case 'open': |
| 85 | + this._onopen_handler.push(handler); |
| 86 | + break; |
| 87 | + |
| 88 | + case 'close': |
| 89 | + this._onclose_handler.push(handler); |
| 90 | + break; |
| 91 | + |
| 92 | + default: |
| 93 | + throw "RPC.on: unknown event'" + event_name + "'"; |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + this.call = function(method, params, success_handler, error_handler) { |
| 98 | + var id = this._message_id; |
| 99 | + this._message_id += 1; |
| 100 | + |
| 101 | + var request = { |
| 102 | + jsonrpc: '2.0', |
| 103 | + id: id, |
| 104 | + method: method, |
| 105 | + params: params |
| 106 | + }; |
| 107 | + |
| 108 | + // default handler |
| 109 | + if(this.DEBUG) { |
| 110 | + this._pending[id] = { |
| 111 | + success_handler: function(data) { |
| 112 | + console.log('RPC success:', data) |
| 113 | + }, |
| 114 | + error_handler: function(data) { |
| 115 | + console.log('RPC error:', data) |
| 116 | + }, |
| 117 | + } |
| 118 | + } else { |
| 119 | + this._pending[id] = { |
| 120 | + success_handler: function(data) {}, |
| 121 | + error_handler: function(data) {}, |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // custom handler |
| 126 | + if(success_handler) |
| 127 | + this._pending[id].success_handler = success_handler; |
| 128 | + |
| 129 | + if(error_handler) |
| 130 | + this._pending[id].error_handler = error_handler; |
| 131 | + |
| 132 | + var request_data = JSON.stringify(request); |
| 133 | + this._ws.send(request_data); |
| 134 | + |
| 135 | + if(this.DEBUG) { |
| 136 | + console.log('RPC >>', request); |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + this.subscribe = function(topic, handler) { |
| 141 | + this.call('subscribe', topic, function(data) { |
| 142 | + if(data.indexOf(topic) > -1 && handler) { |
| 143 | + rpc._topic_handler[topic] = handler; |
| 144 | + } else { |
| 145 | + throw "RPC.subscribe: unknown topic '" + topic + "'"; |
| 146 | + } |
| 147 | + }); |
| 148 | + }; |
| 149 | +} |
0 commit comments