# OpenSwoole Commands / Methods

## 1. **Swoole Server Methods**

### **Creating Server**

- `Swoole\Server __construct(string $host, int $port, int $mode = SWOOLE_PROCESS, int $sock_type = SWOOLE_SOCK_TCP)`
  - Create a new Swoole server instance.

### **Server Configuration**

- `set(array $settings)`
  - Set configuration options for the server.

### **Start Server**

- `start()`
  - Start the server and begin accepting connections.

### **Server Events**

- `on(string $event, callable $callback)`
  - Register event handlers (e.g., `onStart`, `onShutdown`, `onConnect`, `onReceive`).

#### **Common Server Event Handlers:**
- `onStart($server)`
  - Called when the server starts.
  
- `onShutdown($server)`
  - Called when the server shuts down.

- `onConnect($server, $fd, $reactorId)`
  - Triggered when a connection is established.

- `onReceive($server, $fd, $reactorId, $data)`
  - Triggered when data is received from a client.

- `onClose($server, $fd, $reactorId)`
  - Triggered when a connection is closed.

### **Sending Data**

- `send(int $fd, string $data)`
  - Send data to a specific client connection.

- `sendto(string $ip, int $port, string $data)`
  - Send data to a specified IP and port.

- `sendfile(int $fd, string $filename)`
  - Send a file to the client.

### **Stopping Server**

- `shutdown()`
  - Stop the server gracefully.

---

## 2. **Swoole HTTP Server Methods**

### **Creating HTTP Server**

- `Swoole\Http\Server __construct(string $host, int $port, int $mode = SWOOLE_PROCESS, int $sock_type = SWOOLE_SOCK_TCP)`
  - Create a new HTTP server.

### **HTTP Server Event Handlers**

- `on(string $event, callable $callback)`
  - Register HTTP server events (e.g., `onRequest`, `onStart`, `onShutdown`).

#### **Common HTTP Server Event Handlers:**
- `onRequest($request, $response)`
  - Triggered when an HTTP request is received. Allows you to send a response.

- `onStart($server)`
  - Triggered when the HTTP server starts.

- `onShutdown($server)`
  - Triggered when the HTTP server shuts down.

### **Setting HTTP Response Headers**

- `response->header(string $name, string $value, bool $replace = true)`
  - Set a header for the HTTP response.

### **Sending HTTP Response**

- `response->end(string $data)`
  - Send the response back to the client.

- `response->write(string $data)`
  - Write data to the response stream.

### **Stopping HTTP Server**

- `shutdown()`
  - Stop the HTTP server gracefully.

---

## 3. **Swoole WebSocket Server Methods**

### **Creating WebSocket Server**

- `Swoole\WebSocket\Server __construct(string $host, int $port, int $mode = SWOOLE_PROCESS, int $sock_type = SWOOLE_SOCK_TCP)`
  - Create a WebSocket server.

### **WebSocket Server Event Handlers**

- `on(string $event, callable $callback)`
  - Register WebSocket server events (e.g., `onOpen`, `onMessage`, `onClose`).

#### **Common WebSocket Server Event Handlers:**
- `onOpen($server, $request)`
  - Triggered when a WebSocket connection is established.

- `onMessage($server, $frame)`
  - Triggered when a message is received from a WebSocket client.

- `onClose($server, $fd)`
  - Triggered when a WebSocket connection is closed.

### **Sending WebSocket Data**

- `push(int $fd, string $data)`
  - Push data to a WebSocket client.

- `disconnect(int $fd)`
  - Disconnect a WebSocket client.

### **WebSocket Connection Info**

- `getClientInfo(int $fd)`
  - Get information about a connected WebSocket client.

---

## 4. **Swoole Coroutine Methods**

### **Creating Coroutine Tasks**

- `Swoole\Coroutine::create(callable $callback)`
  - Create a new coroutine task.

- `Swoole\Coroutine::sleep(float $seconds)`
  - Make the current coroutine sleep for the specified time.

- `Swoole\Coroutine::yield()`
  - Yield the coroutine and return control to the event loop.

- `Swoole\Coroutine::resume(int $cid)`
  - Resume a suspended coroutine with a given coroutine ID.

---

## 5. **Swoole Timer Methods**

### **Creating Timers**

- `Swoole\Timer::tick(int $interval, callable $callback)`
  - Set a periodic timer to execute a callback every `interval` milliseconds.

- `Swoole\Timer::after(int $delay, callable $callback)`
  - Set a one-time timer to execute a callback after `delay` milliseconds.

### **Canceling Timers**

- `Swoole\Timer::clear(int $timerId)`
  - Clear the specified timer by its ID.

---

## 6. **Swoole Task Methods**

### **Creating Tasks**

- `Swoole\Server::task(mixed $data)`
  - Dispatch a task to worker processes for asynchronous handling.

### **Handling Task Results**

- `Swoole\Server::finish(mixed $data)`
  - Send the result back to the client once the task is complete.

### **Task Event Handlers**

- `onTask($server, $task_id, $from_id, $data)`
  - Triggered when a task is received by a worker process.

- `onFinish($server, $task_id, $data)`
  - Triggered when a task finishes and sends the result back to the client.

---

## 7. **Swoole Client Methods**

### **Creating TCP Client**

- `Swoole\Client __construct(int $sock_type)`
  - Create a TCP client.

### **Client Methods**

- `connect(string $host, int $port, float $timeout)`
  - Connect to the server.

- `send(string $data)`
  - Send data to the server.

- `recv(float $timeout)`
  - Receive data from the server.

- `close()`
  - Close the client connection.

---

## 8. **Swoole MySQL Client Methods**

### **Creating MySQL Client**

- `Swoole\MySQL::__construct()`
  - Create a MySQL client object.

### **MySQL Client Methods**

- `connect(array $config)`
  - Connect to the MySQL database.

- `query(string $sql)`
  - Execute an SQL query.

- `close()`
  - Close the MySQL connection.

---

## 9. **Swoole Redis Client Methods**

### **Creating Redis Client**

- `Swoole\Redis::__construct()`
  - Create a Redis client.

### **Redis Client Methods**

- `connect(string $host, int $port)`
  - Connect to the Redis server.

- `set(string $key, mixed $value)`
  - Set the value of a Redis key.

- `get(string $key)`
  - Get the value of a Redis key.

- `close()`
  - Close the Redis connection.

---

## 10. **Swoole Channel Methods**

### **Creating a Channel**

- `Swoole\Coroutine\Channel::__construct(int $size)`
  - Create a coroutine channel for communication between coroutines.

### **Channel Methods**

- `push(mixed $data)`
  - Push data into the channel.

- `pop()`
  - Pop data from the channel.

- `close()`
  - Close the channel.

---

## 11. **Swoole Event Loop Methods**

### **Starting the Event Loop**

- `Swoole\Event::wait()`
  - Wait for the event loop to run and process events.

---

## Conclusion

This list covers most of the **OpenSwoole functions** and methods available for common use cases such as managing servers, coroutines, tasks, timers, and interacting with Redis, MySQL, and WebSockets. OpenSwoole provides powerful asynchronous capabilities for PHP applications, allowing you to build scalable, high-performance systems with ease.

Let me know if you'd like more details on any specific method or need examples in another language!
