2015-02-03 21:13:27 +01:00
|
|
|
/**
|
|
|
|
* WebSocket transport. Generally needed because browsers can't handle
|
|
|
|
* TCP sockets so we proxy messages from clients via WebSocket into TCP
|
|
|
|
* data packets.
|
|
|
|
* @module bitmessage/net/ws
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var inherits = require("inherits");
|
|
|
|
var WebSocket = require("ws"); // jshint ignore:line
|
|
|
|
var assert = require("../_util").assert;
|
2015-02-06 10:47:40 +01:00
|
|
|
var BaseTransport = require("./base");
|
2015-02-03 21:13:27 +01:00
|
|
|
|
|
|
|
var WebSocketServer = WebSocket.Server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebSocket transport constructor.
|
|
|
|
* @constructor
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
function Transport() {
|
|
|
|
Transport.super_.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(Transport, BaseTransport);
|
|
|
|
|
|
|
|
Transport.prototype.connect = function(opts) {
|
|
|
|
assert(!this._client, "Already connected");
|
|
|
|
this._client = new WebSocket(opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
Transport.prototype.listen = function(opts) {
|
|
|
|
assert(!this._server, "Already listening");
|
|
|
|
this._server = new WebSocketServer(opts);
|
|
|
|
};
|
|
|
|
|
2015-02-06 10:47:40 +01:00
|
|
|
module.exports = Transport;
|