-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwooleHttpTcpSrv.php
30 lines (30 loc) · 1.21 KB
/
SwooleHttpTcpSrv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
//tcp+http 示例
class SwooleHttpTcpSrv extends SwooleSrv {
const LEN_TYPE = 'L'; //长度类型 无符号长整形 L主机序 N网络序
const LEN_SIZE = 4; //包体定字节位
public function __construct($config)
{
parent::__construct($config);
$this->config['type'] = self::TYPE_HTTP;
if(empty($this->config['listen'] )){
$this->config['listen'] = [ //监听其他地址
'tcp'=>[
'type'=> SWOOLE_SOCK_TCP, //不设置默认tcp SWOOLE_SOCK_TCP , SWOOLE_SOCK_UDP
'setting'=>[
'open_length_check' => true,
'package_max_length' => 65536, //64K 最大数据包尺寸 单位为字节
'package_length_type' => self::LEN_TYPE, //无符号长整形
'package_length_offset' => 0, //长度定字节位
'package_body_offset' => self::LEN_SIZE, //包体定字节位
]
],
];
}
}
//组装发送TCP数据 长度+内容
public static function pack_data($send_data)
{
return pack(self::LEN_TYPE, strlen($send_data)) . $send_data;
}
}