<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$http_worker = new Worker("http://0.0.0.0:2345");
$http_worker->onConnect = function ($connection) {
$connection->protocol = '\\Workerman\\Protocols\\Http';
};
\Workerman\Protocols\Http::header('Content-Type:application/json; charset=utf-8');
$http_worker->onWorkerStart = function ($worker) {
// 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
global $db;
$db = new \Workerman\MySQL\Connection('127.0.0.1', '50681', 'root', 'root', 'aqr_develop');
};
// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function ($connection, $data) {
// 通过全局变量获得db实例
global $db;
// 执行SQL
$all_tables = $db->query('show tables');
$connection->send(json_encode($all_tables));
// $connection->send(PHP_SAPI);
};
// 运行worker
Worker::runAll();
设置了header头,无效
\Workerman\Protocols\Http::header('Content-Type:application/json; charset=utf-8'); $connection->send(json_encode($all_tables));
调用header后再调用send
好的,已经成功了。谢谢
请问下,你这边是怎么设置header,我按照还是不可以