Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Created January 29, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/0e0bf79798a45d0b8ca2b0da83a9f4d6 to your computer and use it in GitHub Desktop.
Save andersevenrud/0e0bf79798a45d0b8ca2b0da83a9f4d6 to your computer and use it in GitHub Desktop.
DanialdNishazmi Example
import osjs from 'osjs';
import {name as applicationName} from './metadata.json';
// Our launcher
const register = (core, args, options, metadata) => {
// Create a new Application instance
const proc = core.make('osjs/application', {args, options, metadata});
// Create our first window
const window1 = proc.createWindow();
window1.render($content => {
// Subscribe to the application event we create below
proc.on('data-was-fetched', json => {
$content.innerHTML = JSON.stringify(json); // This will just fill the content of the window with the response
});
});
// Create a second window
const window2 = proc.createWindow();
window2.render($content => {
// Subscribe to the application event we create below
proc.on('data-was-fetched', json => {
$content.innerHTML = JSON.stringify(json); // This will just fill the content of the window with the response
});
});
// Create a HTTP request to the "/get-some-data" endpoint
proc.request('/get-some-data', {method: 'post'})
.then(json => {
// This will emit an event in your application instance that your windows can subscribe to
proc.emit('data-was-fetched', json);
})
.catch(error => alert(error))
return proc;
};
// Creates the internal callback function when OS.js launches an application
osjs.register(applicationName, register);
// Methods OS.js server requires
module.exports = (core, proc) => ({
// When server initializes
init: async () => {
// Create server HTTP endpoint for "/get-some-data"
core.app.post(proc.resource('/get-some-data'), (req, res) => {
res.json({foo: 'bar'});
});
},
// When server starts
start: () => {},
// When server goes down
destroy: () => {},
// When using an internally bound websocket, messages comes here
onmessage: (ws, respond, args) => {
respond('Pong');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment