Skip to content

Instantly share code, notes, and snippets.

@Huruikagi
Last active October 4, 2016 04:13
Show Gist options
  • Select an option

  • Save Huruikagi/0f9d39eeaadecc33907d409795e871c6 to your computer and use it in GitHub Desktop.

Select an option

Save Huruikagi/0f9d39eeaadecc33907d409795e871c6 to your computer and use it in GitHub Desktop.
WordPressプラグイン有効化時にNode.jsでサーバーを起動する ref: http://qiita.com/Huruikagi/items/37f3885e415b9371a2db
<?php
/*
* Plugin Name: WPChat
* Description: WordPressのユーザー情報を利用してチャットができるようにするプラグインです。
* Version: 0.1;
* Author: Huruikagi
* Author URI: https://github.com/Huruikagi
*/
// プラグインが有効になった際の処理
function on_activate() {
// Node.jsでチャット用のサーバーを起動する処理
// エントリポイントのパス
$path = join(DIRECTORY_SEPARATOR, array(__DIR__, 'chat-server', 'express', 'app.js'));
if (PHP_OS === 'WIN32' || PHP_OS === 'WINNT') {
// windowsのとき
$command = "node $path > nul 2>&1";
$fp= popen("start /B cmd /c $command", "r");
pclose($fp);
} else {
// unix系の時
$command = "node $path";
exec("$command >/dev/null 2>&1 &");
}
}
register_activation_hook(__FILE__, 'on_activate');
// プラグインが無効になったときの処理
function on_deactivate() {
// サーバー停止リクエストを投げる
// TODO URLを設定情報から読み出す
file_get_contents("http://localhost:8888/kill");
}
register_deactivation_hook(__FILE__, 'on_deactivate');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment