-
Notifications
You must be signed in to change notification settings - Fork 20.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from debris/autoprovider
Autoprovider
- Loading branch information
Showing
11 changed files
with
629 additions
and
489 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
This file is part of ethereum.js. | ||
ethereum.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
ethereum.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** @file websocket.js | ||
* @authors: | ||
* Marek Kotewicz <[email protected]> | ||
* Marian Oancea <[email protected]> | ||
* @date 2014 | ||
*/ | ||
|
||
/* | ||
* @brief if qt object is available, uses QtProvider, | ||
* if not tries to connect over websockets | ||
* if it fails, it uses HttpRpcProvider | ||
*/ | ||
if (process.env.NODE_ENV !== 'build') { | ||
var WebSocket = require('ws'); // jshint ignore:line | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'build') { | ||
var web3 = require('./web3'); // jshint ignore:line | ||
} | ||
|
||
var AutoProvider = function (userOptions) { | ||
if (web3.haveProvider()) { | ||
return; | ||
} | ||
|
||
// before we determine what provider we are, we have to cache request | ||
this.sendQueue = []; | ||
this.onmessageQueue = []; | ||
|
||
if (navigator.qt) { | ||
this.provider = new web3.providers.QtProvider(); | ||
return; | ||
} | ||
|
||
userOptions = userOptions || {}; | ||
var options = { | ||
httprpc: userOptions.httprpc || 'http://localhost:8080', | ||
websockets: userOptions.websockets || 'ws://localhost:40404/eth' | ||
}; | ||
|
||
var self = this; | ||
var closeWithSuccess = function (success) { | ||
ws.close(); | ||
if (success) { | ||
self.provider = new web3.providers.WebSocketProvider(options.websockets); | ||
return; | ||
} | ||
self.provider = new web3.providers.HttpRpcProvider(options.httprpc); | ||
self.poll = self.provider.poll.bind(self.provider); | ||
self.sendQueue.forEach(function (payload) { | ||
self.provider(payload); | ||
}); | ||
self.onmessageQueue.forEach(function (handler) { | ||
self.provider.onmessage = handler; | ||
}); | ||
}; | ||
|
||
var ws = new WebSocket(options.websockets); | ||
|
||
ws.onopen = function() { | ||
closeWithSuccess(true); | ||
}; | ||
|
||
ws.onerror = function() { | ||
closeWithSuccess(false); | ||
}; | ||
}; | ||
|
||
AutoProvider.prototype.send = function (payload) { | ||
if (this.provider) { | ||
this.provider.send(payload); | ||
return; | ||
} | ||
this.sendQueue.push(payload); | ||
}; | ||
|
||
Object.defineProperty(AutoProvider.prototype, 'onmessage', { | ||
set: function (handler) { | ||
if (this.provider) { | ||
this.provider.onmessage = handler; | ||
return; | ||
} | ||
this.onmessageQueue.push(handler); | ||
} | ||
}); | ||
|
||
module.exports = AutoProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.