Use Dnode to implement a simple example of communication between PHP and Nodejs _node.js

Source: Internet
Author: User
Tags autoload php and require node server

I. Installation of Dnode

1, for Nodejs, performing

Copy Code code as follows:

$ sudo npm install Dnode



2, for PHP, using composer to install Dnode PHP

Execute the following statement to download the composer

Copy Code code as follows:

$ wget Http://getcomposer.org/composer.phar



Create a file Composer.json, and then fill in the following statement,


Copy Code code as follows:

{
"Require": {
"Dnode/dnode": "0.2.0"
}
}



Perform the following statement installation,


Copy Code code as follows:

$ sudo php composer.phar install

Second, the use of Nodejs to create a simple server program, Server.js

Copy Code code as follows:

var Dnode = require (' Dnode ');
var Server = Dnode ({
Zing:function (n, CB) {CB (n * 100)}
});
Server.listen (7070);



third, the use of PHP to create a client program client.php, which need to refer to the Dnode folder just installed file autoload.php


Copy Code code as follows:



<?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 so provides us all methods
From the server
$remote->zing (function ($n) use ($connection) {
echo "n = {$n}\n";
Once we have the result we can close the connection
$connection->end ();
});
});
$loop->run ();
?>




Four, the implementation of server-side
Copy Code code as follows:

$ node Server.js



v. Execute client-side Invoke server program


Copy Code code as follows:

$ PHP client.php



This invokes the server-side addition program, and then outputs the result


Copy Code code as follows:

n = 3300

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.