ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 基本流程 [TOC] ## 简单的HTTP REST服务器 1、Ports.php中开启Http端口 ``` $config['ports'][] = [ 'socket_type' => PortManager::SOCK_HTTP, 'socket_name' => '0.0.0.0', 'socket_port' => 8081, 'route_tool' => 'NormalRoute' ]; ``` 2、app/AppServer.php中可以做服务器的初始化工作。 3、app/Controllers目录中新建一个AppController.php继承Controller 4、可以写一些逻辑了 ```php class AppController extends Controller { /** * @var AppModel */ public $AppModel; /** * http测试 */ public function http_test() { $this->AppModel = $this->loader->model('AppModel', $this); $this->http_output->end($this->AppModel->test()); } public function http_test_task() { $AppTask = $this->loader->task('AppTask'); $AppTask->testTask(); $AppTask->startTask(function ($serv, $task_id, $data) { $this->http_output->end($data); }); } } ``` 5、我们启动服务器,访问http://localhost:8081/AppController/test ## 简单的TCP服务器 1、需要在ports配置中添加你想用的端口。 ``` $config['ports'][] = [ 'socket_type' => PortManager::SOCK_TCP, 'socket_name' => '0.0.0.0', 'socket_port' => 9091, 'pack_tool' => 'LenJsonPack', 'route_tool' => 'NormalRoute', ]; ``` 2、app/AppServer.php中可以做服务器的初始化工作。 3、app/Controllers目录中新建一个AppController.php继承Controller 4、可以写一些逻辑了 ```php class AppController extends Controller { public function test() { $this->send('helloworld'); } } ``` ## 简单的Websocket服务器 1、需要在ports配置中添加你想用的端口。 ``` $config['ports'][] = [ 'socket_type' => PortManager::SOCK_WS, 'socket_name' => '0.0.0.0', 'socket_port' => 8083, 'route_tool' => 'NormalRoute', 'pack_tool' => 'NonJsonPack', 'opcode' => PortManager::WEBSOCKET_OPCODE_TEXT ]; ``` 2、app/AppServer.php中可以做服务器的初始化工作,和握手Auth 3、app/Controllers目录中新建一个AppController.php继承Controller 4、可以写一些逻辑了 ```php class AppController extends Controller { public function test() { $this->send('helloworld'); } } ```