wormhole/lib/Socket.js

29 lines
821 B
JavaScript
Raw Normal View History

const constants = require("./constants");
2021-01-26 12:04:53 +02:00
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
2021-01-26 12:04:53 +02:00
this._socket.on('data', (e) => {
if (this._state !== constants.states.OPEN) return;
2021-01-26 12:04:53 +02:00
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;
};
2021-01-26 12:04:53 +02:00
module.exports = Socket;