forked from LinkedDestiny/swoole-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
feb1ea1
commit 746d68a
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
class Client | ||
{ | ||
private $client; | ||
|
||
|
||
public function __construct() { | ||
$this->client = new swoole_client(SWOOLE_SOCK_TCP); | ||
} | ||
|
||
public function connect() { | ||
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) { | ||
echo "Error: {$fp->errMsg}[{$fp->errCode}]\n"; | ||
} | ||
|
||
$msg_normal = "This is a Msg"; | ||
$msg_eof = "This is a Msg\r\n"; | ||
$msg_length = pack("N" , strlen($msg_normal) ). $msg_normal; | ||
|
||
$i = 0; | ||
while( $i < 100 ) { | ||
$this->client->send( $msg_length ); | ||
$i ++; | ||
} | ||
} | ||
} | ||
|
||
$client = new Client(); | ||
$client->connect(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
'package_max_length' => 8192, | ||
'open_eof_check'=> true, | ||
'package_eof' => "\r\n" | ||
)); | ||
|
||
$this->serv->on('Start', array($this, 'onStart')); | ||
$this->serv->on('Connect', array($this, 'onConnect')); | ||
$this->serv->on('Receive', array($this, 'onReceive')); | ||
$this->serv->on('Close', array($this, 'onClose')); | ||
|
||
$this->serv->start(); | ||
} | ||
|
||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
|
||
public function onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
|
||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
$data_list = explode("\r\n", $data); | ||
foreach ($data_list as $msg) { | ||
if( !empty($msg) ) { | ||
echo "Get Message From Client {$fd}:{$msg}\n"; | ||
} | ||
|
||
} | ||
} | ||
|
||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
} | ||
|
||
new Server(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $http; | ||
|
||
public function __construct() { | ||
$this->http = new swoole_http_server("127.0.0.1", 9501); | ||
|
||
$this->http->set( | ||
array( | ||
'worker_num' => 16, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 1 | ||
) | ||
); | ||
|
||
$this->http->on('Start', array($this, 'onStart')); | ||
$this->http->on('request' , array( $this , 'onRequest')); | ||
$this->http->start(); | ||
} | ||
|
||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
|
||
public function onRequest($request, $response) { | ||
$response->end("<h1>Hello Swoole.</h1>"); | ||
} | ||
} | ||
|
||
new Server(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
'package_max_length' => 8192, | ||
'open_length_check'=> true, | ||
'package_length_offset' => 0, | ||
'package_body_offset' => 4, | ||
'package_length_type' => 'N' | ||
)); | ||
|
||
$this->serv->on('Start', array($this, 'onStart')); | ||
$this->serv->on('Connect', array($this, 'onConnect')); | ||
$this->serv->on('Receive', array($this, 'onReceive')); | ||
$this->serv->on('Close', array($this, 'onClose')); | ||
|
||
$this->serv->start(); | ||
} | ||
|
||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
|
||
public function onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
|
||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
$length = unpack("N" , $data)[1]; | ||
echo "Length = {$length}\n"; | ||
$msg = substr($data,-$length); | ||
echo "Get Message From Client {$fd}:{$msg}\n"; | ||
} | ||
|
||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
} | ||
|
||
new Server(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2 | ||
)); | ||
|
||
$this->serv->on('Start', array($this, 'onStart')); | ||
$this->serv->on('Connect', array($this, 'onConnect')); | ||
$this->serv->on('Receive', array($this, 'onReceive')); | ||
$this->serv->on('Close', array($this, 'onClose')); | ||
|
||
$this->serv->start(); | ||
} | ||
|
||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
|
||
public function onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
|
||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
echo "Get Message From Client {$fd}:{$data}\n"; | ||
} | ||
|
||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
} | ||
|
||
new Server(); |