1111
1212
1313use FastD \Http \Cookie ;
14+ use LogicException ;
15+ use RuntimeException ;
1416use swoole_client ;
1517use swoole_http_client ;
1618
@@ -109,7 +111,7 @@ public function createRequest($url, $async = false, $keep = false)
109111 $ socketType = SWOOLE_SOCK_UDP ;
110112 break ;
111113 default :
112- throw new \ LogicException ("Don't support schema " .$ info ['scheme ' ]);
114+ throw new LogicException ("Don't support schema " .$ info ['scheme ' ]);
113115 }
114116
115117 $ this ->path = isset ($ info ['path ' ]) ? $ info ['path ' ] : '/ ' ;
@@ -274,43 +276,38 @@ public function configure($configure)
274276 * @param swoole_client $client
275277 * @return mixed
276278 */
277- public function connect (swoole_client $ client )
278- {
279- }
279+ public function onConnect (swoole_client $ client ) {}
280280
281281 /**
282282 * @param swoole_client $client
283283 * @param string $data
284284 * @return mixed
285285 */
286- public function receive (swoole_client $ client , $ data )
287- {
288- }
286+ public function onReceive (swoole_client $ client , $ data ) {}
289287
290288 /**
291289 * @param swoole_client $client
292290 * @return mixed
293291 */
294- public function error (swoole_client $ client )
295- {
296- }
292+ public function onError (swoole_client $ client ) {}
297293
298294 /**
299295 * @param swoole_client $client
300296 * @return mixed
301297 */
302- public function close (swoole_client $ client )
303- {
304- }
298+ public function onClose (swoole_client $ client ) {}
305299
306300 /**
307301 * @param string|array $data
308302 * @return mixed
309303 */
310304 public function send ($ data = '' )
311305 {
306+ if (null === $ this ->client ) {
307+ throw new LogicException ('Please call the createRequest method first ' );
308+ }
312309 if ( ! $ this ->client ->connect ($ this ->host , $ this ->port , $ this ->timeout )) {
313- throw new \ RuntimeException (socket_strerror ($ this ->client ->errCode ));
310+ throw new RuntimeException (socket_strerror ($ this ->client ->errCode ));
314311 }
315312
316313 $ this ->client ->send ($ this ->wrapBody ($ data ));
@@ -326,16 +323,16 @@ public function send($data = '')
326323 public function start ()
327324 {
328325 $ this ->client ->on ("connect " , function ($ client ) {
329- call_user_func_array ([$ this , 'connect ' ], [$ client ]);
326+ call_user_func_array ([$ this , 'onConnect ' ], [$ client ]);
330327 });
331328 $ this ->client ->on ("receive " , function ($ client , $ data ) {
332- call_user_func_array ([$ this , 'receive ' ], [$ client , $ data ]);
329+ call_user_func_array ([$ this , 'onReceive ' ], [$ client , $ data ]);
333330 });
334331 $ this ->client ->on ("error " , function ($ client ) {
335- call_user_func_array ([$ this , 'error ' ], [$ client ]);
332+ call_user_func_array ([$ this , 'onError ' ], [$ client ]);
336333 });
337334 $ this ->client ->on ("close " , function ($ client ) {
338- call_user_func_array ([$ this , 'close ' ], [$ client ]);
335+ call_user_func_array ([$ this , 'onClose ' ], [$ client ]);
339336 });
340337 $ this ->client ->connect ($ this ->host , $ this ->port , $ this ->timeout );
341338 }
0 commit comments