国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用DNode實現php和nodejs之間通信的簡單實例

2019-11-20 12:06:06
字體:
來源:轉載
供稿:網友

一、安裝DNode

1, for nodejs, 執行

復制代碼 代碼如下:

$ sudo npm install dnode

2, for php, 利用composer來安裝DNode php

執行下列語句下載composer

復制代碼 代碼如下:

創建一個文件composer.json,然后填入如下語句,
復制代碼 代碼如下:

{
    "require": {
        "dnode/dnode": "0.2.0"
    }
}

執行如下語句安裝,

復制代碼 代碼如下:

$ sudo php composer.phar install

二、利用nodejs創建簡單server程序, server.js

復制代碼 代碼如下:

var dnode = require('dnode');
var server = dnode({
    zing: function (n, cb) { cb(n * 100) }
});
server.listen(7070);

三、利用php創建客戶端程序client.php, 其中需要引用剛才安裝的dnode文件夾里面的文件autoload.php
復制代碼 代碼如下:

<?php
// Connect to DNode server running in port 7070 and call
// Zing with argument 33
require 'lib/vendor/autoload.php';


// This is the class we're exposing to DNode
class Temp
{
    // Compute the client's temperature and stuff that value into the callback
    public function temperature($cb)
    {
    }
}

$loop = new React/EventLoop/StreamSelectLoop();
$dnode = new DNode/DNode($loop, new Temp());
$dnode->connect(7070, function($remote, $connection) {
    // Remote is a proxy object that provides us all methods
    // from the server
    $remote->zing(33, function($n) use ($connection) {
        echo "n = {$n}/n";
        // Once we have the result we can close the connection
        $connection->end();
    });
});
$loop->run();
?>


四、執行服務器端
復制代碼 代碼如下:

$ node server.js

五、執行客戶端調用服務端程序
復制代碼 代碼如下:

$ php client.php

這會調用服務器端的加法程序,然后輸出結果
復制代碼 代碼如下:

n = 3300

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 忻州市| 旬阳县| 平泉县| 兰溪市| 湛江市| 普兰店市| 浦县| 华亭县| 民勤县| 娄底市| 台江县| 蕉岭县| 甘谷县| 武隆县| 武胜县| 六枝特区| 曲沃县| 秭归县| 宽城| 景德镇市| 茂名市| 夏河县| 和硕县| 新闻| 双流县| 米脂县| 剑川县| 贡嘎县| 柳州市| 资阳市| 宝丰县| 庐江县| 新源县| 忻州市| 堆龙德庆县| 海南省| 合川市| 伊宁县| 鹤山市| 尚义县| 大名县|