yunitto
Version:
Yunitto, the unique NodeOS desktop manager
118 lines (99 loc) • 3.35 kB
JavaScript
var express = require("express");
var app = express();
var uuid = require("uuid");
var ee = require('event-emitter');
var WebSocketServer = require('ws').Server;
var Yunitto = function(opts) { // initialize the interfaces
this.screens = [];
this.e = ee({});
this.detectScreens = false;
this.debug = opts.debug;
if(!opts.nobrowser) {
if(!opts.httpport) {
opts.httpport = 52758;
}
if(!opts.sockport) {
opts.sockport = 28844;
}
app.use(express.static(__dirname + '/b_back'));
app.listen(opts.httpport);
this.wss = new WebSocketServer({ port: opts.sockport });
this.wss.on('connection', (ws) => {
if(this.debug) {
console.log("[Yunitto] Websocket connection established. ");
}
if(this.detectScreens) {
var id = uuid.v4();
ws.on('close', () => {
if(this.debug) {
console.log("[Yunitto] Websocket connection died. ");
}
this.e.emit('destroyScreen', this.screens[id]);
delete this.screens[id];
});
this.screens[id] = new this.Screen("browser", [ws], this);
} else {
// TODO add user to pending screens list and make it be confirmed
}
});
}
};
Yunitto.prototype.useAllScreens = function() {
this.detectScreens = true;
};
var Screen = function(interf, params, y) {
// init interface
this.interf = interf;
this.map = [];
this.type = "canvas";
if(this.interf === "framebuffer") {
} else if(this.interf === "egl") {
} else if(this.interf === "browser") {
this.ws = params[0];
if(this.debug) {
console.log("[Yunitto][Screen] Browser screen created. ");
}
} else {
throw "Invalid interface requested";
}
y.e.emit('loadScreen', this);
};
Screen.prototype.setContentType = function(type) {
this.type = type;
if(this.debug) {
console.log("[Yunitto][Screen] Content-type set to " + type + ". ");
}
};
Screen.prototype.render = function(content) {
if(this.type == "canvas") {
if(this.interf === "framebuffer") {
} else if(this.interf === "egl") {
} else if(this.interf === "browser") {
this.ws.send("3" + JSON.stringify(content.func));
} else {
throw "Invalid interface being used for rendering";
}
} else if(this.type == "html") {
if(this.interf === "framebuffer") {
} else if(this.interf === "egl") {
} else if(this.interf === "browser") {
if(content.url) {
this.ws.send("1" + content.url);
} else if(content.html) {
this.ws.send("2" + content.html);
}
} else {
throw "Invalid interface being used for rendering";
}
} else {
throw "Invalid type being used for rendering";
}
};
Yunitto.prototype.Screen = Screen;
Yunitto.prototype.useScreen = function(interf, params) {
this.screens.push(new this.Screen(interf, params));
if(this.debug) {
console.log("[Yunitto] Now using a new screen. ");
}
};
module.exports = Yunitto;