This article describes how to use DNode to implement communication between php and nodejs. This article describes how to install DNode and how to use it. For more information, see
1. Install DNode
1, for nodejs, run
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
The Code is as follows:
$ Wget http://getcomposer.org/composer.phar
Create a file composer. json and enter the following statement,
The Code is as follows:
{
"Require ":{
"Dnode/dnode": "0.2.0"
}
}
Run the following statement to install the SDK,
The Code is as follows:
$ Sudo php composer. phar install
Ii. Use nodejs to create a simple server program, server. js
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. You must reference the file autoload. php In the dnode folder you just installed.
The Code is 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 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 ();
?>
Iv. Execution Server
The Code is as follows:
$ Node server. js
5. Execute the client to call the server program
The Code is as follows:
$ Php client. php
This will call the addition program on the server side and then output the result.
The Code is as follows:
N = 3300