Coder Social home page Coder Social logo

swoole's Introduction

php_swoole

Swoole written by C, based on the Linux epoll does not rely on any third-party libraries, as a the PHP extensions running high-performance network server framework, you can easily maintain more than 100,000 concurrent TCP connections. Swoole provides a full asynchronous, non-blocking, parallel PHP Socket Server to achieve. Support UDP, TCP, IPv6 support multi-port monitoring, multi timer and operation mode can be configured. PHP developers do not care about the underlying implementation, only need to use PHP to write the callback function, write the business logic code can be. The network can be used for server-side development, such as WebSocket Server, Web server, FTP server.

Document 中文

feature

  • Event-driven
  • Full asynchronous non-blocking
  • No lock design
  • Separate read and write
  • Concurrent execution. Support Multi-Thread or Multi-Process
  • Support IPv6

Web Application Server

https://github.com/matyhtf/swoole_framework

example

server.php . See examples/server.php

<?php
$serv = swoole_server_create("127.0.0.1", 9500, SWOOLE_THREAD, SWOOLE_SOCK_TCP);

swoole_server_set($serv, array(
    'timeout' => 2.5,  //select and epoll_wait timeout. 
    'poll_thread_num' => 2, //reactor thread num
    'writer_num' => 2,     //writer thread num
    'worker_num' => 4,    //worker process num
    'backlog' => 128,   //listen backlog
));

/*
argv0  server resource
argv1  listen host
argv2  listen port
argv3  sock_type  SWOOLE_SOCK_TCP or SWOOLE_SOCK_TCP6 or SWOOLE_SOCK_UDP or SWOOLE_SOCK_UDP6
*/
swoole_server_addlisten($serv, "127.0.0.1", 9501, SWOOLE_SOCK_UDP);
function my_onStart($serv)
{
    echo "Server:start\n";
}

function my_onShutdown($serv)
{
    echo "Server:onShutdown\n";
}

function my_onTimer($serv, $interval)
{
    echo "Server:Timer Call.Interval=$interval \n";
}

function my_onClose($serv,$fd,$from_id)
{
	echo "Client:Close. fd=$fd|from_id=$from_id\n";
}

function my_onConnect($serv,$fd,$from_id)
{
	echo "Client:Connect. fd=$fd|from_id=$from_id\n";
}

function my_onReceive($serv, $fd, $from_id, $data)
{
	echo "Client:Data. fd=$fd|from_id=$from_id|data=$data\n";
	swoole_server_send($serv, $fd, "Server: $data");
	//swoole_server_send($serv, $other_fd, "Server: $data", $other_from_id);
	//swoole_server_close($serv, $fd, $from_id);
	//swoole_server_close($serv, $ohter_fd, $other_from_id);
}

swoole_server_handler($serv, 'onStart', 'my_onStart');
swoole_server_handler($serv, 'onConnect', 'my_onConnect');
swoole_server_handler($serv, 'onReceive', 'my_onReceive');
swoole_server_handler($serv, 'onClose', 'my_onClose');
swoole_server_handler($serv, 'onShutdown', 'my_onShutdown');
swoole_server_handler($serv, 'onTimer', 'my_onTimer');
swoole_server_addtimer($serv, 2);
swoole_server_addtimer($serv, 10);
swoole_server_start($serv);
?>

client.php . See examples/client.php

<?php
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
$client->connect('127.0.0.1', 9500, 0.5, 0);
for($i=0; $i < 1000; $i++){
    $client->send("hello world-{$i}");
    $data = $client->recv(1024, 0);
    echo $data;
}
$client->close();
php server.php

telnet 127.0.0.1 9500
hello
server: hello

Use in C

See examples/server.c

cmake .
make

blog

http://swoole.sinaapp.com/ 中文(Chinese)

swoole's People

Contributors

matyhtf avatar taobao-php avatar recoye avatar shenzhe avatar

Watchers

James Cloos avatar ruanxianhuo avatar

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.