12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- // Server
- class Server
- {
- private $serv;
- public function __construct() {
- $this->serv = new swoole_server("0.0.0.0", 9099);
- $this->serv->set(array(
- 'worker_num' => 8,
- 'daemonize' => false,
- 'max_request' => 10000,
- 'dispatch_mode' => 2,
- 'debug_mode'=> 1,
- // 'open_length_check' => true,
- //'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 ) {
- $serv->send( $fd, "Hello {$fd}!" );
- }
- public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
- //$data = iconv('GBK','UTF-8', $data);
- //$str = base64_encode($data);
- //$str = unpack('h*', $str);
- //print_r($str) . "\n";
- $data = bin2hex($data);
- echo "客户端响应 {$fd}:{$data}\n";
- fwrite(STDOUT,"请输入:");
- $str = trim(fgets(STDIN));
- //$str = pack('H*', $str);
- $str = str_replace(' ', '', $str);
- $str = hex2bin($str);
- if($str)
- {
- $serv->send( $fd, $str );
- }
- }
- public function onClose( $serv, $fd, $from_id ) {
- echo "Client {$fd} close connection\n";
- }
- }
- // 启动服务器
- $server = new Server();
|