Skip to content

Instantly share code, notes, and snippets.

@nukosuke
Created January 28, 2017 15:32
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 nukosuke/1ac7baadadbda2ff290a0d6f86e468d6 to your computer and use it in GitHub Desktop.
Save nukosuke/1ac7baadadbda2ff290a0d6f86e468d6 to your computer and use it in GitHub Desktop.
use IO::Socket::UNIX;
use JSON;
my $SOCK_PATH = "./node.sock";
print $SOCK_PATH;
my $client = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Peer => $SOCK_PATH,
) or die $!;
$client->send(qq|
(function() {
console.log(this);
return this.PerlReact.serverRender({ name: 'MyComponent', domNodeId: 'pl-react', props: {} });
}).call(this);
|, 0);
my $max_int = 2**30-1;
my $buf;
my $result_json = "";
$client->recv($buf, $max_int);
$result_json .= $buf;
print decode_json($result_json)->{'html'};
$client->close;
var net = require('net');
var fs = require('fs');
var bundlePath = './node/';
var bundleFileName = 'registry.js';
var currentArg;
function Handler() {
this.queue = [];
this.initialized = false;
}
Handler.prototype.handle = function (connection) {
console.log("handle");
var callback = function () {
console.log("callback");
connection.setEncoding('utf8');
connection.on('data', (data)=> {
console.log('Processing request: ' + data);
var result = eval(data);
console.log(result);
connection.write(result);
});
};
if (this.initialized) {
callback();
} else {
this.queue.push(callback);
}
};
Handler.prototype.initialize = function () {
console.log('Processing ' + this.queue.length + ' pending requests');
var callback;
while (callback = this.queue.pop()) {
callback();
}
this.initialized = true;
};
var handler = new Handler();
process.argv.forEach((val) => {
if (val[0] == '-') {
currentArg = val.slice(1);
return;
}
if (currentArg == 's') {
bundleFileName = val;
}
});
try {
fs.mkdirSync(bundlePath);
} catch (e) {
if (e.code != 'EEXIST') throw e;
}
fs.watchFile(bundlePath + bundleFileName, (curr) => {
if (curr && curr.blocks && curr.blocks > 0) {
if (handler.initialized) {
console.log('Reloading server bundle must be implemented by restarting the node process!');
return;
}
require(bundlePath + bundleFileName);
console.log('Loaded server bundle: ' + bundlePath + bundleFileName);
handler.initialize();
}
});
var unixServer = net.createServer(function (connection) {
handler.handle(connection);
});
unixServer.listen('node.sock');
process.on('SIGINT', () => {
unixServer.close();
process.exit();
});
require(bundlePath + bundleFileName);
handler.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment