wormhole/lib/Socket.js

29 lines
No EOL
821 B
JavaScript

const constants = require("./constants");
class Socket {
constructor({ initialState='CONNECTING', socket }) {
this._state = initialState;
this._socket = socket;
this._accepted = false;
this._fateDecided = false; // Wether the decision to accept or reject the socket was made
this._socket.on('data', (e) => {
if (this._state !== constants.states.OPEN) return;
console.log(e.toString());
});
}
}
Socket.prototype._setConnectionState = function(state) {
this._state = state;
};
Socket.prototype._setAccepted = function(state) {
if (this._fateDecided) throw new Error('Tried to decide fate (wether socket is accepted or not) more than 1 time');
this._fateDecided = true;
this._accepted = state;
};
module.exports = Socket;