swoole-1.7.9 增加了內(nèi)置的websocket服務(wù)器支持,通過(guò)幾行PHP代碼就可以寫(xiě)出一個(gè)異步非阻塞多進(jìn)程的WebSocket服務(wù)器。
常見(jiàn)使用場(chǎng)景:我們?cè)谑褂胮hp開(kāi)發(fā)的時(shí)候,原生最不好用的是socket類(lèi)庫(kù)了,而在開(kāi)發(fā)IM和及時(shí)通信項(xiàng)目是,我們現(xiàn)在有了新的選擇后端使用php Swoole WebSocket + 前端 html5 WebSocket;
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->on('open', function (swoole_websocket_server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (swoole_websocket_server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
swoole_websocket_server 繼承自 swoole_http_server
http 400
錯(cuò)誤頁(yè)面Swoole\Http\Client
作為WebSocket客戶(hù)端甚至你還可以結(jié)合使用socket.io來(lái)配合開(kāi)發(fā)
更多建議: