wormhole/lib/Socket.js

34 lines
987 B
JavaScript
Raw Normal View History

const constants = require("./constants");
2021-02-02 10:17:49 +02:00
const parser = require('./Parser');
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-02-02 10:17:49 +02:00
this._decodePayload(e.buffer);
2021-01-26 12:04:53 +02:00
});
}
}
2021-02-02 10:17:49 +02:00
Socket.prototype._decodePayload = function(payload) {
console.log(parser.parseWebsocketFrame(new DataView(payload)));
};
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;