Home >Backend Development >PHP Tutorial >The problem that the onclose and onconnect interfaces are not called in the swoole extension of PHP
I encountered a problem when using swoole extension to write an online chat example. I checked a lot of information and now record it here.
By looking at the interface document of swoole_server, there is a clear comment in the callback registration interface on:
* swoole_server->on & swoole_http_server->on are the same except swoole_http_server : * - not accepting onConnect/onReceive callback accept events onRequest
swoole_http_server and swoole_server are generally the same, but the connect and receive interfaces are not called in swoole_http_server, and the request interface is called accordingly. .
In the small chat example, swoole_websocket_server is used. Through testing, the receive interface in swoole_websocket_server has not been called. However, as a long connection service, the callbacks in
close and connect in the worker are still necessary. For example, online broadcast notifications and offline broadcast notifications require callbacks to these two corresponding interfaces to implement them well. So the official definitely supports the callback.
The problem was finally found through a swoole version update announcement.
swoole-1.7.16 version has been released, BUG repair version:
Add swoole_server->tick and swoole_timer_tick functions
Add http server support for gzip compression
Add swoole_table->incr/decr atomic increment/decrement Method
Add open_eof_split configuration, use EOF detection to support automatic subcontracting
Add server statistical items request_count and worker_request_count
Add server connection iterator, you can use foreach to traverse all connections of the server
Add query_string for http server request
Add http server multipart -Support for form and uploaded files
Fix the BUG of onReceive data merging failure
Fix the BUG of conflict between swoole_server->addtimer and tick timer
Fix the problem that Accept is not set to block under low version Linux
Fix Accept failure to return Too Many Connection duplication Log printing problem
Fixed the problem of invalid task_max_request parameter
Fixed the problem of invalid waitall parameter of swoole_client
Fixed the BUG of infinite loop in swoole_table
The second parameter of the WebSocket server onOpen callback function is adjusted from $fd to the $request object
Http server allows sending Empty body response
Disable swoole_websocket_server->send method
BASE mode supports sending data to any FD
Set dispatch_mode = 1, 3 and then close the onClose/onConnect event callback
Allow setting non-system reserved signals in the Worker process
Remove The underlying dependence of swoole on object resource attributes directly reads pointers to improve performance
Solve the problem that the heartbeat thread cannot forcibly kill legacy connections
Optimize the dispatch_mode=3 mode to improve the efficiency of task allocation
This is a problem with the worker allocation mode. In preemption mode and polling mode, these two callback interfaces are no longer called. In fixed mode, each client's data packet will be processed by a fixed worker process, so that some private information belonging to this client can be stored in the worker process, and some frequently read and written data can be cached, which is similar to the process dictionary in Erlang. operate. In this way, the client needs to do some cleaning operations when it goes offline. Online initialization operation. So this model is very meaningful and necessary. Fixed mode is suitable for handling situations where each client's logic is relatively uniform.
The above introduces the problem that the onclose and onconnect interfaces are not called in the swoole extension of PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.