bitmessage-js/docs/net_ws.js.html

299 lines
11 KiB
HTML
Raw Normal View History

2015-02-12 11:44:15 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: net/ws.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: net/ws.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* WebSocket transport. Needed because browsers can't handle TCP sockets
* so we use separate WebSocket server to proxy messages into TCP data
* packets. Available for both Node.js and Browser platforms.
* **NOTE**: `WsTransport` is exported as a module.
2015-02-14 11:54:39 +01:00
* @example var WsTransport = require("bitmessage/lib/net/ws");
2015-02-14 12:00:58 +01:00
* @module bitmessage/net/ws
2015-02-12 11:44:15 +01:00
*/
"use strict";
var objectAssign = Object.assign || require("object-assign");
var inherits = require("inherits");
var WebSocket = require("ws"); // jshint ignore:line
var assert = require("../_util").assert;
var PPromise = require("../platform").Promise;
var structs = require("../structs");
var messages = require("../messages");
var BaseTransport = require("./base");
var WebSocketServer = WebSocket.Server;
2015-02-24 18:23:55 +01:00
var ServicesBitfield = structs.ServicesBitfield;
2015-02-12 11:44:15 +01:00
var getmsg = BaseTransport._getmsg;
var unmap = BaseTransport._unmap;
/**
2015-02-14 12:06:56 +01:00
* WebSocket transport class. Implements
* [base transport interface]{@link
* module:bitmessage/net/base.BaseTransport}.
2015-02-12 11:44:15 +01:00
* @param {Object=} opts - Transport options
* @param {Array} opts.seeds - Bootstrap nodes (none by default)
* @param {Object} opts.services -
* [Service features]{@link module:bitmessage/structs.ServicesBitfield}
2015-02-24 18:23:55 +01:00
* provided by this node (`NODE_MOBILE` for Browser and `NODE_MOBILE` +
* `NODE_GATEWAY` for Node by default)
2015-02-12 11:44:15 +01:00
* @param {(Array|string|Buffer)} opts.userAgent -
* [User agent]{@link module:bitmessage/user-agent} of this node
2015-02-24 18:23:55 +01:00
* (user agent of bitmessage library by default)
* @param {number[]} opts.streams - Streams accepted by this node ([1]
* by default)
* @param {number} opts.port - Incoming port of this node, makes sence
* only on Node platform (18444 by default)
2015-02-12 11:44:15 +01:00
* @constructor
* @static
*/
function WsTransport(opts) {
WsTransport.super_.call(this);
objectAssign(this, opts);
this.seeds = this.seeds || [];
2015-02-24 18:23:55 +01:00
this.services = this.services || ServicesBitfield().set([
ServicesBitfield.NODE_MOBILE,
ServicesBitfield.NODE_GATEWAY,
]);
this.streams = this.streams || [1];
this.port = this.port || 18444;
2015-02-12 11:44:15 +01:00
}
inherits(WsTransport, BaseTransport);
WsTransport.prototype._sendVersion = function() {
return this.send(messages.version.encode({
services: this.services,
userAgent: this.userAgent,
2015-02-24 18:23:55 +01:00
streams: this.streams,
2015-02-12 11:44:15 +01:00
port: this.port,
remoteHost: this._client._socket.remoteAddress,
remotePort: this._client._socket.remotePort,
}));
};
WsTransport.prototype._handleTimeout = function() {
var client = this._client;
// TODO(Kagami): We may also want to close connection if it wasn't
// established within minute.
client._socket.setTimeout(20000);
client._socket.on("timeout", function() {
client.close();
});
this.on("established", function() {
// Raise timeout up to 10 minutes per spec.
// TODO(Kagami): Send ping frame every 5 minutes as PyBitmessage.
client._socket.setTimeout(600000);
});
};
2015-02-24 18:23:55 +01:00
WsTransport.prototype._setupClient = function(client, incoming) {
2015-02-12 11:44:15 +01:00
var self = this;
self._client = client;
var verackSent = false;
var verackReceived = false;
var established = false;
client.on("open", function() {
// NOTE(Kagami): This handler shouldn't be called at all for
2015-02-24 18:23:55 +01:00
// incoming connections but let's be sure.
if (!incoming) {
2015-02-12 11:44:15 +01:00
// NOTE(Kagami): We may set timeout only after connection was
// opened because socket may not yet be available when
// `_setupClient` is called.
self._handleTimeout();
self.emit("open");
self._sendVersion();
}
});
client.on("message", function(data, flags) {
var decoded;
if (!flags.binary) {
// TODO(Kagami): Send `error` message and ban node for some time
// if there were too many errors?
2015-02-24 18:23:55 +01:00
return self.emit("warning", new Error("Peer sent non-binary data"));
2015-02-12 11:44:15 +01:00
}
try {
decoded = structs.message.decode(data);
} catch (err) {
return self.emit("warning", new Error(
2015-02-24 18:23:55 +01:00
"Message decoding error: " + err.message
2015-02-12 11:44:15 +01:00
));
}
2015-02-24 18:23:55 +01:00
self.emit("message", decoded.command, decoded.payload);
2015-02-12 11:44:15 +01:00
});
// High-level message processing.
2015-02-24 18:23:55 +01:00
self.on("message", function(command, payload) {
var version;
var veropts = incoming ? {mobile: true} : {gateway: true};
2015-02-12 11:44:15 +01:00
if (!established) {
if (command === "version") {
if (verackSent) {
return;
}
2015-02-24 18:23:55 +01:00
try {
version = self._decodeVersion(payload, veropts);
} catch(err) {
self.emit("error", err);
return client.close();
}
2015-02-12 11:44:15 +01:00
self.send("verack");
verackSent = true;
2015-02-24 18:23:55 +01:00
if (incoming) {
2015-02-12 11:44:15 +01:00
self._sendVersion();
} else if (verackReceived) {
established = true;
2015-02-24 18:23:55 +01:00
self.emit("established", version);
2015-02-12 11:44:15 +01:00
}
} else if (command === "verack") {
verackReceived = true;
if (verackSent) {
established = true;
2015-02-24 18:23:55 +01:00
self.emit("established", version);
2015-02-12 11:44:15 +01:00
}
}
}
});
client.on("error", function(err) {
self.emit("error", err);
});
client.on("close", function() {
self.emit("close");
delete self._client;
});
};
WsTransport.prototype.bootstrap = function() {
return PPromise.resolve([].concat(this.seeds));
};
/**
* Connect to a WebSocket node. Connection arguments are the same as for
* [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
*/
WsTransport.prototype.connect = function(address, protocols, options) {
assert(!this._client, "Already connected");
assert(!this._server, "Already listening");
// `new` doesn't work with `apply`, so passing all possible arguments
// manually.
this._setupClient(new WebSocket(address, protocols, options));
};
/**
* Listen for incoming WebSocket connections. Listen arguments are the
* same as for
* [WebSocketServer](https://github.com/websockets/ws#server-example).
* Available only for Node platform.
*/
WsTransport.prototype.listen = function(options, callback) {
assert(!this._client, "Already connected");
assert(!this._server, "Already listening");
var self = this;
var server = self._server = new WebSocketServer(options, callback);
server.on("connection", function(client) {
2015-02-24 18:23:55 +01:00
var opts = objectAssign({}, self);
delete opts._server;
var transport = new self.constructor(opts);
var incoming = true;
transport._setupClient(client, incoming);
2015-02-12 11:44:15 +01:00
transport._handleTimeout();
2015-02-24 18:23:55 +01:00
var addr = client._socket.remoteAddress;
var port = client._socket.remotePort;
2015-02-12 11:44:15 +01:00
self.emit("connection", transport, unmap(addr), port);
});
server.on("error", function(err) {
self.emit("error", err);
});
2015-02-24 18:23:55 +01:00
// `ws` doesn't emit "close" event by default.
server._server.on("close", function() {
self.emit("close");
delete self._server;
});
2015-02-12 11:44:15 +01:00
};
WsTransport.prototype.send = function() {
if (this._client) {
// TODO(Kagami): `mask: true` doesn't work with Chromium 40. File a
// bug to ws bugtracker.
this._client.send(getmsg(arguments), {binary: true});
} else {
throw new Error("Not connected");
}
};
WsTransport.prototype.broadcast = function() {
var data = getmsg(arguments);
if (this._server) {
this._server.clients.forEach(function(client) {
client.send(data, {binary: true});
});
} else {
throw new Error("Not listening");
}
};
WsTransport.prototype.close = function() {
if (this._client) {
this._client.close();
} else if (this._server) {
this._server.close();
}
};
module.exports = WsTransport;
</code></pre>
</article>
</section>
</div>
<nav>
2015-02-14 12:00:58 +01:00
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bitmessage.html">bitmessage</a></li><li><a href="module-bitmessage_address.html">bitmessage/address</a></li><li><a href="module-bitmessage_crypto.html">bitmessage/crypto</a></li><li><a href="module-bitmessage_messages.html">bitmessage/messages</a></li><li><a href="module-bitmessage_net_base.html">bitmessage/net/base</a></li><li><a href="module-bitmessage_net_tcp.html">bitmessage/net/tcp</a></li><li><a href="module-bitmessage_net_ws.html">bitmessage/net/ws</a></li><li><a href="module-bitmessage_objects.html">bitmessage/objects</a></li><li><a href="module-bitmessage_pow.html">bitmessage/pow</a></li><li><a href="module-bitmessage_structs.html">bitmessage/structs</a></li><li><a href="module-bitmessage_user-agent.html">bitmessage/user-agent</a></li><li><a href="module-bitmessage_wif.html">bitmessage/wif</a></li></ul><h3>Classes</h3><ul><li><a href="module-bitmessage_address.Address.html">Address</a></li><li><a href="module-bitmessage_net_base.BaseTransport.html">BaseTransport</a></li><li><a href="module-bitmessage_net_tcp.TcpTransport.html">TcpTransport</a></li><li><a href="module-bitmessage_net_ws.WsTransport.html">WsTransport</a></li><li><a href="module-bitmessage_structs.PubkeyBitfield.html">PubkeyBitfield</a></li><li><a href="module-bitmessage_structs.ServicesBitfield.html">ServicesBitfield</a></li></ul><h3>Namespaces</h3><ul><li><a href="module-bitmessage_messages.addr.html">addr</a></li><li><a href="module-bitmessage_messages.error.html">error</a></li><li><a href="module-bitmessage_messages.getdata.html">getdata</a></li><li><a href="module-bitmessage_messages.inv.html">inv</a></li><li><a href="module-bitmessage_messages.version.html">version</a></li><li><a href="module-bitmessage_objects.broadcast.html">broadcast</a></li><li><a href="module-bitmessage_objects.getpubkey.html">getpubkey</a></li><li><a href="module-bitmessage_objects.msg.html">msg</a></li><li><a href="module-bitmessage_objects.pubkey.html">pubkey</a></li><li><a href="module-bitmessage_structs.encrypted.html">encrypted</a></li><li><a href="module-bitmessage_structs.inv_vect.html">inv_vect</a></li><li><a href="module-bitmessage_structs.message.html">message</a></li><li><a href="module-bitmessage_structs.net_addr.html">net_addr</a></li><li><a href="module-bitmessage_structs.object.html">object</a></li><li><a href="module-bitmessage_structs.var_int.html">var_int</a></li><li><a href="module-bitmessage_structs.var_int_list.html">var_int_list</a></li><li><a href="module-bitmessage_structs.var_str.html">var_str</a></li></ul>
2015-02-12 11:44:15 +01:00
</nav>
<br class="clear">
<footer>
2015-02-24 18:23:55 +01:00
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0-dev</a> on Tue Feb 24 2015 20:23:33 GMT+0300 (MSK)
2015-02-12 11:44:15 +01:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>