Code Monkey home page Code Monkey logo

socket-tcp-php-client's Introduction

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 [email protected] , i wanna to update this code to be better.

socket-tcp-php-client's People

Contributors

viyancs avatar

Stargazers

 avatar  avatar zhiqiang.qiu avatar  avatar Martin Bazik avatar  avatar Ralf Blumenthal avatar

Watchers

 avatar

Forkers

ewintec

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.