Home > Web Front-end > JS Tutorial > body text

A simple example of using DNode to implement communication between php and nodejs_node.js

WBOY
Release: 2016-05-16 15:51:17
Original
1087 people have browsed it

1. Install DNode

1, for nodejs, execute

Copy code The code is as follows:

$ sudo npm install dnode

2, for php, use composer to install DNode php

Execute the following statement to download composer

Copy code The code is as follows:

Create a file composer.json and fill in the following statements,
Copy code The code is as follows:

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

Execute the following statement to install,

Copy code The code is as follows:

$ sudo php composer.phar install

2. Use nodejs to create a simple server program, server.js

Copy code The code is as follows:

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

3. Use PHP to create the client program client.php, which needs to reference the file autoload.php in the dnode folder you just installed
Copy code The code is as follows:

// 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 ReactEventLoopStreamSelectLoop();
$dnode = new DNodeDNode($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();
?>


4. Execute server side
Copy code The code is as follows:

$ node server.js

5. Execute the client to call the server program
Copy code The code is as follows:

$ php client.php

This calls the server-side addition routine and prints the result
Copy code The code is as follows:

n = 3300
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!