Use DNode to implement simple instance _ node. js for communication between php and nodejs

Source: Internet
Author: User
Tags autoload node server
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

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.