Gönderen Konu: Flash+php Ile Chat Server Olayı  (Okunma sayısı 5809 defa)

Flash+php Ile Chat Server Olayı

« : 25.03.2004 00:04:06 »
Hızlı düğmeleri aç

spaztica

İleti: 1.493

Çevrimdışı
  • Administrator
  • *****
  • Hero Member
    • Profili Görüntüle
php rutin:

Kod: [Seç]
#!/usr/local/bin/php -q

<?php 

set_time_limit 
(0); 
ob_implicit_flush (); 

define ('SOCKET_READ'1024); 
define ('LISTEN_QUEUE'10); 
define ('PORT'9999); 
define ('MAX_CLIENTS'30); 
define ('MAX_ROOMS'4); 

//--------------------------------------------- 

function room_join ($room

global 
$client$count$room$rooms$room_position
if (!isset (
$rooms)) 
 { 
 for (
$room_build_count 0$room_build_count MAX_ROOMS$room_build_count++) 
  
$rooms [$room_build_count] = array (); 
 } 
$room_size count ($rooms [$room]); 
if (
$room_size 4
 { 
 
array_push ($rooms [$room], $count); 
 
$room_position [$count] = array ($room$room_size); 
 
socket_write ($client [$count], &quot;You have joined room &quot; . $room . &quot;.&quot; . chr (0)); 
 
socket_write ($client [$count], $room . &quot;#&quot; . ($room_size + 1) . chr (0)); 
 

else 
 
socket_write ($client [$count], &quot;Room &quot; . $room . &quotis currently full.&quot; . chr (0)); 


//--------------------------------------------- 

function room_exit () 

global 
$client$count$rooms$room_position
array_splice ($rooms [$room_position [$count] [0]], $room_position [$count] [1], 1); 
socket_write ($client [$count], &quot;You have left room &quot; . $room_position [$count] [0] . &quot;.&quot; . chr (0)); 
$room_position [$count] = null


//--------------------------------------------- 

function kill_server () 

global 
$listenfd$client
for (
$count 0$count MAX_CLIENTS$count++) 
 { 
 if (
$client [$count] != null
  { 
  
socket_write ($client [$count], &quot;Server disconnected!&quot; . chr (0)); 
  
socket_close ($client [$count]); 
  } 
 } 
socket_close ($listenfd); 
exit; 


//--------------------------------------------- 

function close_client ($count

global 
$client$usernames$remote_host$remote_port
if (isset (
$rooms)) 
 
room_exit (); 
for (
$broadcast_count 0$broadcast_count <= MAX_CLIENTS$broadcast_count++) 
 { 
 if (
$broadcast_count == $count
  
socket_write ($client [$count], &quot; <font color=&quot;#CC0000&quot;>You have disconnected.</font>&quot; . chr (0)); 
 
else if ($client [$broadcast_count] != null
  
socket_write ($client [$broadcast_count], &quot; <font color=&quot;#CC0000&quot;>&quot; . $usernames [$count] . &quot; has disconnected.</font>&quot; . chr (0)); 
 

socket_close ($client [$count]); 
$client [$count] = null
unset (
$remote_host [$count]); 
$client_still_connected false
for (
$count 0$count <= MAX_CLIENTS$count++) 
 { 
 if (
$client [$count] != null
  { 
  
$client_still_connected true
  break; 
  } 
 } 
if (
$client_still_connected == false
 
kill_server (); 


//--------------------------------------------- 

$listenfd socket_create (AF_INETSOCK_STREAM0); 
if (
$listenfd
 print &
quot;Listening on port &quot; . PORT . &quot;
&
quot;; 
else 
 { 
 
socket_write ($client [$count], &quot;Socket died!&quot; . chr (0)); 
 die (&
quot;Socket died!
&
quot;); 
 } 
socket_setopt ($listenfdSOL_SOCKETSO_REUSEADDR0); 
if (!
socket_bind ($listenfd, &quot;127.0.0.1&quot;, PORT)) 
 { 
 
socket_close ($listenfd); 
 die (&
quot;Could not bind!
&
quot;); 
 } 
socket_listen ($listenfdLISTEN_QUEUE); 
for (
$count 0$count MAX_CLIENTS$count++) 
 
$client [$count] = null
while (
1
 { 
 
$rfds [0] = $listenfd
  { 
  for (
$count 0$count <= MAX_CLIENTS$count++) 
   if (
$client [$count] != null
    
$rfds [$count 1] = $client [$count]; 
  } 
 
$nready socket_select ($rfds$null$nullnull); 
 if (
in_array ($listenfd$rfds)) 
  { 
  for (
$count 0$count <= MAX_CLIENTS$count++) 
   { 
   if (
$client [$count] == null
    { 
    
$client [$count] = socket_accept ($listenfd); 
    
socket_setopt ($client [$count], SOL_SOCKETSO_REUSEADDR0); 
    
socket_getpeername ($client [$count], $remote_host [$count], $remote_port [$count]); 
    if (
$count MAX_CLIENTS
     { 
     break; 
     } 
    } 
   if (
$count >= MAX_CLIENTS
    { 
    
close_client ($count); 
    break; 
    } 
   } 
  if (--
$nready <= 0
   continue; 
  } 
 for (
$count 0$count <= MAX_CLIENTS$count++) 
  { 
  if (
$client [$count] == null
   continue; 
  if (
in_array ($client [$count], $rfds)) 
   { 
   
$incoming_data trim (socket_read ($client [$count], SOCKET_READ)); 
   if (!
$incoming_data
    
close_client ($count); 
   else 
    { 
    if (
substr ($incoming_data01) == &quot;#&quot;) 
     

     
$call_function explode (&quot;#&quot;, $incoming_data); 
     
if ($call_function [1] == &quot;client_number&quot;) 
      
socket_write ($client [$count], $count chr (0)); 
     else if (
$call_function [1] == &quot;username&quot;) 
      { 
      
$usernames [$count] = $call_function [2]; 
      for (
$broadcast_count 0$broadcast_count <= MAX_CLIENTS$broadcast_count++) 
       { 
       if ((
$client [$broadcast_count] != null) && ($broadcast_count != $count)) 
        
socket_write ($client [$broadcast_count], &quot; <font color=&quot;#CC0000&quot;>&quot; . $usernames [$count] . &quot; has connected.</font>&quot; . chr (0)); 
       

      } 
     else if (
$call_function [1] == &quot;room_join&quot;) 
      { 
      
$room $call_function [2]; 
      
room_join ($room); 
      } 
     else if (
$call_function [1] == &quot;room_exit&quot;) 
      
room_exit (); 
     else if (
$call_function [1] == &quot;close_client&quot;) 
      
close_client ($count); 
     else if (
$call_function [1] == &quot;kill_server&quot;) 
      
kill_server (); 
     } 
    else 
     { 
     for (
$broadcast_count 0$broadcast_count <= MAX_CLIENTS$broadcast_count++) 
      { 
      if (
$broadcast_count == $count
       
socket_write ($client [$count], &quot;<font color=&quot;#999999&quot;>You said: &quot; . $incoming_data . chr (0)); 
      
else if ($client [$broadcast_count] != null
       
socket_write ($client [$broadcast_count], $usernames [$count] . &quot;: &quot; . $incoming_data chr (0)); 
      } 
     } 
    } 
   if (--
$nready <= 0
    break; 
   } 
  } 
 } 
?>


flash kısmı için de test_chat.fla dosyasını çekin.