Skip to content

implementation socket TCP client using PHP to communication to nodejs socket server

Notifications You must be signed in to change notification settings

viyancs/socket-tcp-php-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

socket-tcp-php-client

this repository is communication using protocol tcp with the server using nodejs(net required) and the client using php, before you use this repository i'm recommended to read this article TCP introduction.

requiretment


1) nodejs
2) php5-sockets

Running The Application

  • install nodejs

  • install php5-sockets

  • install IDE(recomended Netbeans because the repository using netbeans)

  • run the server that been use nodejs

  • for the server code

    var net = require('net');
    var server = net.createServer(function (socket) {
    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
    socket.on('data', function(data) {    
        //var datas = JSON.parse(data); //if you receive json must be parse 
        console.log('DATA ' + socket.remoteAddress + ': ' + data);  
        socket.write("you said: " + data + '\r\n');
        socket.end();
    });
    socket.on('connect', function() {
        console.log("client already connected");
    });
    socket.on('end', function() {
        console.log('DONE');
    });
    });
    server.listen(1338, '127.0.0.1');
    console.log("server is listen on 1338");

save to app.js run with sudo node app.js
or in windows you can use node.exe app.js

  • run in terminal with php TCPClient.php
    define('__LIB__', dirname(dirname(__FILE__)));
    require_once(__LIB__ . '/php/library/TCPConnection.php');
    require_once(__LIB__ . '/php/library/TCPCallback.php');

    class TCPClient implements TCPCallback {
            public $socket;
        public function index() {
            $this->socket = new TCPConnection("127.0.0.1", 1338, $this);
            $data = array("username" => "foo", "pass" => "bar");
            $this->socket->connect();
            //$socket->send("testing socket TCP dari viyancs"); // format string only
            //$socket->emit("login",  array((object)($data),  (object)($data1))); //multy dimension json
            $this->socket->emit("login", array($data)); //one array dimension json
        }

    public function onConnect() {
        echo "socket is connected 
"; echo "-------------------------------------------------------------
"; } public function onDisconnect() { echo "socket is disconnected
"; echo "-------------------------------------------------------------
"; } public function onError($err) { echo "something wrong : " .$err; } public function onJSON($json) { echo "receive data from server
"; echo "=============================================================
"; echo "the data is " .$json. '
'; echo "=============================================================
"; } public function onJSONEvent($event, $jsonArray) { echo "receive data from server
"; echo "=============================================================
"; echo "the event is " .$event. '
'; foreach ($jsonArray as $key => $value) { echo "Key: $key;
"; var_dump($value); echo '
'; } echo "=============================================================
"; } public function onMessage($message) { echo "message from sever :" .$message; } public function onSend($msg) { echo "sending data to server
"; echo "=============================================================
"; echo 'data = ' .$msg .'
'; echo "=============================================================
"; } } $client = new TCPClient(); $client->index();

Change Log

Version 1.0 Release Note;

Features

  • send data using string format
  • send data using JSONFormat (JSONObject,JSONArray) and dynamical JSON.
  • receive data using string format
  • receive data using json format

Bugging

  • problem when receive data from server after connected to server

Licence

if you want to use this repository please don't remove comment in each code, fork and follow this repository if any question send email to msofyancs@gmail.com , i wanna to update this code to be better.

About

implementation socket TCP client using PHP to communication to nodejs socket server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages