-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswoole
More file actions
296 lines (200 loc) · 5.8 KB
/
Copy pathswoole
File metadata and controls
296 lines (200 loc) · 5.8 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
安装 swoole
pecl install swoole
'0.0.0.0' 监听所有的ip
service 服务端
TCP服务器
//$event = new swoole_server(string $host, int $port, int $mode = SWOOLE_PROCESS, int $sock_type = SWOOLE_SOCK_TCP);
$serv = new swoole_server('0.0.0.0', 9501);
// 配置参数
$serv->set([
'worker_num' => 3, // cpu 的 1~4 倍
'max_request' => 10000
]);
// 连接事件
$serv->on('connect', function($serv, $fd, $reactor_id){
echo $reactor_id . " | " . $fd . " | connect\n";
});
// 接收事件
$serv->on('receive', function($serv, $fd, $from_id, $data){
$serv->send($fd, "Server: " . $fd . "| from_id " . $from_id . " | " . $data);
});
// 关闭事件
$serv->on('close', function($serv, $fd){
echo "close\n";
});
// 开启服务器
$serv->start();
bool swoole_server->on(strint $event, mixed $callback);
$event->on('connect', function($serv, $fd){})
connect 建立连接的时候
$serv 服务器 句柄
$fd 客户端 获取到的用户的信息
receive 当接受到数据
$serv
$fd
$from_id 客户端id
$data 传递的数据
close 关闭连接
$serv
$fd
$event->start() //启动服务器
http_server 服务
// 创建 http 服务
$http = new swoole_http_server('0.0.0.0', 8811);
$http->on('request', function ($request, $response) {
print_r($request->get);
$response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
// 开启服务
$http->start();
client
// 连接 swoole tcp 服务器
$client = new swoole_client(SWOOLE_SOCK_TCP);
if(!$client->connect('127.0.0.1', 9501)){
echo '连接失败';
exit;
}
// php cli常量 句柄操作
fwrite(STDOUT, '请输入消息:');
$msg = trim(fgets(STDIN));
// 发送消息给 tcp server 服务器
$client->send($msg);
//发送数据
bool swoole_server->end(int $fd, string $data, int $reactorThreadld = 0);
// 接收来自server 的数据
$result = $client->recv();
echo $result;
UDP服务器
$serv = new swoole_server();
$cock_type = SWOOLE_SOCK_UDP
//监听数据的事件
$serv->on('packet', function($serv, $data, $fd){
//发送数据
$serv->sendto($fd['address'], $fd['port'], "server:$data")
})
onPacket(swoole_server $server, string $data, array $client_info);
bool swoole server->sendto(string $ip, int $port, string $data, int $service
web服务器
new swoole_http_server();
swoole_http_server 集成来自 swoole_server
参数1:string $host 监听ip地址
参数2:int $port 监听端口
on/start函数
$serv = new swoole_http_server('0.0.0.0', 9501);
//$request 请求信息
//$response 返回信息
$serv->on('request', function($request, $response){
$response->header('Content-Type', 'text/html;charset=utf-8');
$response->end('hello world');
});
$serv->start();
web_socket
new swoole_websocket_server()
swoole_websocket_server 继承swoole_http_server
on/start 函数
open/message/close
push() 发送数据
服务端
$ws = new swoole_websocket_server($host, $port);
//建立连接 $ws服务器, $request客户端信息
$ws->on('open', function($ws, $request){})
//接收信息
$ws->on('message', function($ws, $request){
$request->data
$ws->push($request->fd)
})
//关闭连接
$ws->on('close', function($ws, $request){})
$ws->start()
客户端
js
var wsServer = 'ws://192.168.50.133:9501';
var webSocket = new WebSocket(wsServer);
webSocket.onopen = function(evt){}
webSocket.onclose = function(evt){}
webSocket.onmessaage = function(evt){}
webSocket.onerror = function(evt){}
异步TCP服务器
task() 投递异步任务
on('事件', function(){}) 处理函数 执行异步函数
finish() 任务处理完成后结果
//创建tcp服务器
$serv = new swoole_server('0,0,0,0', 9501);
//设置异步 进程工作数
$serv->set(['task_worker_num'] => 4);
//投递异步任务
$serv->on('receive', function($serv,$fd,$from_id,$data){
$task_id = $serv->task($data); //异步id
})
//处理异步任务
$serv->on('task', function($serv,$task_id,$from_id,$data){
$serv->finish('ok');
}
//处理结果
$serv->on('finish',function($ser,$task_id,))
tcp客户端
//创建tcp客户端
new swoole_client(SWOOLE_SOCK_TCP)
异步tcp客户端
new swoole_client()
SWOOLE_SOCK_TCP //TCP协议
SWOOLE_SOCK_ASYNC //异步支持
//创建异步TCP客户端
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
//注册链接成功的回调
$client->on('connet', function($cli){
$cli->send("hello");
})
//注册数据接收
$client->on('reseive', function($cli, $data){
echo $data;
})
//注册链接失败
$client->on('error', function($cli){
echo '失败';
})
//注册关闭函数
$client->on('close', function($cli){
})
//发起链接
$client->connet('192.168.50.1', 8080, 10)
Tack 任务
onTask
onFinish
设置 work 连接数
定时器
循环触发 swoole_timer_tick
参数1 int $after_time_ms 指定时间 毫秒
参数2 mixed $callback function 执行的函数
参数3 mixed $user_param 用户参数
清除定时器
bool swoole_timer_clear
单次触发 swoole_timer_after
// 定时器
wsoole_timer_tick(2000, function($timer_id){})
// 延时器
swoole_timer_after(3000, function($timer_id){})
异步IO操作
读取文件
// 默认读取 4m
swoole_async_readfile('file_path', function($filename, $filecontent));
// 可用 分段读取 来读取大文件
// 写文件
swoole_async_writefile('file_path', $content, function($filename){
echo 'ok';
}, FILE_APPEND);
// 超过 4m 可使用分段写
配置参数
$server->set([
'worker_num' => 8,
// 启动的 worker 进程数 建议为 cpu核数的1~4倍
'max_request' => 8,
// 每个 worker 最大任务数 防止worker 大量请求后内存溢出
'max_conn' => 1000
// 最大 TCP 连接数
'ipc_mode' => 1
// 设置进程间的通信方式
1 使用unix socket通信
2 使用消息队列通信
3 使用消息队列通信, 并设置为争抢模式
])