Node/Dnode - Js

D-Node – Part – II

Run DNode Examples:

  1. You will get some example inside D:\wamp\www\MYPATH\dnode_sample\examples (while extracting dnode_sample zip)

  2. For each example have client and server files. First we need to run Server file then need to run client file.

  3. Here for example are running simple folder files. Its in inside MYPATH\dnode_sample\examples\simple

Open command prompt 1 and run server.php, open another command prompt run client.php, refer screen shot- 7.

  1. Similarly you can check other examples its in inside

D:\wamp\www\MYAPTH\dnode_sample\examples\

  1. Bidirectional

  2. \phpcr\simple

  3. Example code for helloworld. Copy client and server.php files and create one folder called “HelloWorld”

Keep client and server files inside this folder.

  1. Please check code below the screen shot

  2. 7.     We can run it from browser also.

Screen shot – 7

 screenshot-7

 

Code for Hello world:

Example Hello world Program:

Client.php

<?php

// Include Composer-generated autoloader

require(__DIR__.’/../../vendor/autoload.php’);

$loop = new React\EventLoop\StreamSelectLoop();

// Connect to DNode server running in port 7070 and call Show_helloworld with Mytext

$dnode = new DNode\DNode($loop);

$dnode->connect(7070, function($remote, $connection) {

                $mytext = “Hello World”;

    $remote->show_helloworld($mytext, function($n) use ($connection) {

        echo “n = {$n}\n”;

        $connection->end();

    });

});

$loop->run();

Server.php

<?php

// Include Composer-generated autoloader

require(__DIR__.’/../../vendor/autoload.php’);

// This is the class we’re exposing to DNode

class Helloworld

{

    // Public methods are made available to the network

    public function show_helloworld($n, $cb)

    {

        // Dnode is async, so we return via callback

        $cb($n);

    }

}

$loop = new React\EventLoop\StreamSelectLoop();

// Create a DNode server

$server = new DNode\DNode($loop, new Helloworld());

$server->listen(7070);

$loop->run();

Note: Class (ie: Helloworld) and function name (show_helloworld) should be different, don’t use same name, if you use it will give error.

How to run Dnode files in Browser:

If you want run Dnode files in browser then we need to comment one function in below file path. If you disable this function. Because download code is giving this issue. If you check the code you will get idea, function close() is used in two places (Ie: readable and writable) but while calling this first readable and writable calling so conflict will happen better comment close() in readable file.

Ex: class Stream extends EventEmitter implements ReadableStreamInterface, WritableStreamInterface

Problem: abstract (previously declared etc… issue)

If you face any issue like “abstract previously declared ….. “

Solution: D:\wamp\www\MYPATH\dnode_sample\vendor\react\stream\React\Stream\ReadableStreamInterface.php è disable public function close ()