2017 © Pedro Peláez
 

library socket

Socket server and client

image

ga/socket

Socket server and client

  • Friday, January 20, 2017
  • by liuzexin
  • Repository
  • 1 Watchers
  • 1 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

GASocket is a easy(with block) for PHP socket communication.

Introduction

The SocketClient class is used to create a socket communication from server.SocketServer class is used to listen the server port and create socket communication for client request., (*1)

GASocket mainly use the 'block' to receive callback.socket_* function is the core of GASocket., (*2)

When you use the GASocket as server, sever receive the quit data form clinet, this client connection will disconnect.Or receive the shutdown data from client, server will close in listening port and program execution is completed., (*3)

Installation

composer require ga/socket dev-master

Usage

namespace

\ga\socket, (*4)

SocketConnection

Config Params ip : Connection ip address. port : Port. callback : When client receive the server's data ,this block will call. domain, type, protocol : According to socket_create() function to set up.(Default is TCP connection configuration). sendTimeOut,recTimeOut : According to [socket_set_option()][2] function to set up.(), these two params in seconds., (*5)

SocketClient Extends SocketConnection

Config Params sync : If true it was blocking mode.Default is false means it was 'nonblock' mode.
sendData : When connection is successful send the sendData., (*6)

EXAMPLE 1:, (*7)

$soc = new SocketClient([
    'ip' => '127.0.0.1',
    'port' => '30000'
]);
$soc->sendData = 'data' . "\n";
$soc->callback = function($data, $socket) use ($soc){
    echo 'Client receive:' . $data ."\n";
};
$soc->start();

NOTICE In the client $soc is the same of $socket., (*8)

SocketServer Extends SocketConnection

Config Params singleMode : The true means that the max number of clients is only one. maxClients : The max number of the clients to connect.(If singleMode was true ,this param is invalid, and it's default value is 10.), (*9)

EXAMPLE 2:, (*10)

$soc = new SocketServer([
    'singleModel' =>true
]);
$soc->callback = function ($data, $socket) use($soc){

    echo 'server receive:' . $data ."\n";
    $soc->send($socket, 'Server has receive data'."\n\r");
};
$soc->start();

NOTICE $soc is the server socket variable, $socket is the current connected client socket., (*11)

The Versions

20/01 2017

dev-master

9999999-dev

Socket server and client

  Sources   Download

null

by Avatar liuzexin

server client socket