Skip to content

Commit 547f2bc

Browse files
committed
异步TCP客户端
1 parent 2612553 commit 547f2bc

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

AsyTCPClient.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
// 创建异步TCP客户端
3+
$client = new swoole_client(SWOOLE_COCK_TCP, SWOOLE_SOCK_ASYNC);
4+
5+
// 注册连接成功的回调
6+
$client->on('connect', function ($cli) {
7+
$cli->send('hello \n');
8+
});
9+
10+
// 注册数据接收 $cli 服务端信息 $data 数据
11+
$client->on('receive', function ($cli, $data) {
12+
echo "data: $data";
13+
});
14+
15+
// 注册连接失败
16+
$client->on('error', function ($cli) {
17+
echo '失败';
18+
});
19+
20+
// 注册关闭函数
21+
$client->on('close', function ($cli) {
22+
echo '关闭\n';
23+
});
24+
25+
//发起连接
26+
$client->connect('192.168.1.31', 8080, 10);

0 commit comments

Comments
 (0)