如何用PHP實(shí)現(xiàn)Socket服務(wù)器
想要構(gòu)建聊天應(yīng)用,或者甚至是游戲嗎?那么,socket服務(wù)器將成為你邁出的***步。一旦你了解了創(chuàng)建服務(wù)器的基本功能,那么后續(xù)的優(yōu)化步驟就會(huì)變得同樣簡單。
socket服務(wù)器的工作方式是這樣的,不間斷地運(yùn)行以等待客戶端的連接。一旦客戶端連接上了,服務(wù)器就會(huì)將它添加到客戶名單中,然后開始等待來自客戶端的消息。
不要走開,下面是完整的源代碼:
- // Set time limit to indefinite execution
- set_time_limit (0);
- // Set the ip and port we will listen on
- $address = 'localhost';
- $port = 10000;
- $max_clients = 10;
- // Array that will hold client information
- $client = Array();
- // Create a TCP Stream socket
- $sock = socket_create(AF_INET, SOCK_STREAM, 0);
- // Bind the socket to an address/port
- socket_bind($sock, $address, $port) or die('Could not bind to address');
- // Start listening for connections
- socket_listen($sock);
- echo "Waiting for connections...\r\n";
- // Loop continuously
- while (true) {
- // Setup clients listen socket for reading
- $read[0] = $sock;
- for ($i = 0; $i < $max_clients; $i++) {
- if (isset($client[$i]['sock']))
- $read[$i + 1] = $client[$i]['sock'];
- }
- // Set up a blocking call to socket_select()
- if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = 5) < 1)
- continue;
- /* if a new connection is being made add it to the client array */
- if (in_array($sock, $read)) {
- for ($i = 0; $i < $max_clients; $i++) {
- if (empty($client[$i]['sock'])) {
- $client[$i]['sock'] = socket_accept($sock);
- echo "New client connected $i\r\n";
- break;
- }
- elseif ($i == $max_clients - 1)
- echo "Too many clients...\r\n";
- }
- } // end if in_array
- // If a client is trying to write - handle it now
- for ($i = 0; $i < $max_clients; $i++) { // for each client
- if (isset($client[$i]['sock'])) {
- if (in_array($client[$i]['sock'], $read)) {
- $input = socket_read($client[$i]['sock'], 1024);
- if ($input == null) {
- echo "Client disconnecting $i\r\n";
- // Zero length string meaning disconnected
- unset($client[$i]);
- } else {
- echo "New input received $i\r\n";
- // send it to the other clients
- for ($j = 0; $j < $max_clients; $j++) {
- if (isset($client[$j]['sock']) && $j != $i) {
- echo "Writing '$input' to client $j\r\n";
- socket_write($client[$j]['sock'], $input, strlen($input));
- }
- }
- if ($input == 'exit') {
- // requested disconnect
- socket_close($client[$i]['sock']);
- }
- }
- } else {
- echo "Client disconnected $i\r\n";
- // Close the socket
- socket_close($client[$i]['sock']);
- unset($client[$i]);
- }
- }
- }
- } // end while
- // Close the master sockets
- socket_close($sock);
啊呀,乍一看這似乎是一個(gè)大工程,但是我們可以先將它分解為幾個(gè)較小的部分。***部分是創(chuàng)建服務(wù)器。Lines:2至20。
這部分代碼設(shè)置了變量、地址、端口、***客戶端和客戶端數(shù)組。接下來創(chuàng)建socket并將其綁定到我們指定的地址和端口上。
下面我們要做的事情就是執(zhí)行一個(gè)死循環(huán)(實(shí)際上我們是故意的!)。Lines:22至32。在這部分代碼中我們做的***步是設(shè)置 $read 數(shù)組。此數(shù) 組包含所有客戶端的套接字和我們主服務(wù)器的套接字。這個(gè)變量稍后會(huì)用于select語句:告訴PHP監(jiān)聽來自這些客戶端的每一條消息。
socket_select()的***一個(gè)參數(shù)告訴我們的服務(wù)器在返回值之前最多等待5秒鐘。如果它的返回值小于1,那么就表示沒有收到任何數(shù)據(jù),所以只需要返回循環(huán)頂部,繼續(xù)等待。
腳本的下一個(gè)部分,是增加新的客戶端到數(shù)組中。Lines:33至44。
將新的客戶端放置在列表的末尾。檢查以確??蛻舳说臄?shù)量沒有超過我們想要服務(wù)器處理的數(shù)量。
下面要介紹的代碼塊相當(dāng)大,也是服務(wù)器的主要部分。當(dāng)客戶端將消息發(fā)送到服務(wù)器時(shí),就需要這塊代碼挺身而出來處理。消息可以是各種各樣的,斷開消息、實(shí)際斷開——只要是服務(wù)器需要處理的消息。Lines:46至末尾。
代碼循環(huán)通過每個(gè)客戶端并檢查是否收到來自于它們的消息。如果是,獲取輸入的內(nèi)容。根據(jù)輸入來檢查這是否是一個(gè)斷開消息,如果是那就從數(shù)組中刪除它們,反之,那它就是一個(gè)正常的消息,那我們的服務(wù)器再次通過所有客戶端,并一個(gè)一個(gè)寫信息給他們,跳過發(fā)送者。
好了,下面試試創(chuàng)造你自己的聊天服務(wù)器吧!