Web Programming >> PHP Programming
|
[목차] |
제19장 대화방 만들기(MySQL)
8. 공유메모리를 이용한 대화방
1. PHP에서 공유메모리 지원시키기 [root@home1 php-4.1.1]# ./configure --with-apxs=/usr/local/httpd/bin/apxs --with-mysql=/usr/local/mysql --enable-track-vars --disable-debug --enable-sysvsem --enable-sysvshm --enable-shared --with-config-file-path=/usr/local/httpd/conf
[root@home1 php-4.1.1]# make [root@home1 php-4.1.1]# make install [root@home1 php-4.1.1]# cp php.ini-dist /usr/local/httpd/conf/php.ini
[root@home1 php-4.1.1]# cd /usr/local/httpd/conf [root@home1 conf]# vi php.ini 생략... output_buffering = 0ff 으로 해야 합니다.
2. 공유메모리 관련 함수들
sem_get - Get a semaphore id sem_acquire - Acquire a semaphore sem_release - Release a semaphore shm_attach - Creates or open a shared memory segment shm_detach - Disconnects from shared memory segment shm_remove - Removes shared memory from Unix systems shm_put_var - Inserts or updates a variable in shared memory shm_get_var - Returns a variable from shared memory shm_remove_var - Removes a variable from shared memory
3. 공유메모리를 이용한 대화방 만들기
파일명: index.html <HTML> <HEAD> <TITLE>채팅방</TITLE> <frameset rows="0%,70%,30%" frameborder=0 border=0> <frame src="null.html" name="hidden"> <frame src="./message.html" name="up"> <frame src="./login.html" name="down"> </frameset> </HTML>
파일명: login.html <?php
if ($action != 'login' || !$nicName) { echo(" <html> <head><link rel='stylesheet' href='./style.css' type='text/css'></head> <body class='mainText' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'> <form name='form1' action='$PHP_SELF' method='post'> <input type='hidden' name='action' value='login'> 대화명 입력 : <input type='text' name='nicName'> </form> </body> </html> "); exit; }
echo (" <html> <head> <link rel='stylesheet' href='./style.css' type='text/css'> <script Language='javascript'> top.up.location.href='view.html?name=$nicName'; </script> </head> <body class='mainText' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'> <form name='form1' action='putMessage.html' method='post' target='hidden'> <input type='hidden' name='name' value='$nicName'> 내용 : <input type='text' size='30' name='comment'> </form> <script Language='javascript'> document.form1.comment.focus(); </script> </body> </html> ");
$shm_key = 0x9999; $semid = sem_get($shm_key,10); sem_acquire($semid); $shmid = shm_attach($shm_key,46084);
$message = "<font size='2' color='black'>[방장 공지] {$nicName}님께서 입장하셨습니다.</font>"; shm_put_var ($shmid,1,$message); // 초기 공지를 저장
shm_detach ($shmid); sem_release($semid); ?>
파일명: message.html <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=euc-kr"> </head> <body bgcolor="#EEEEEE" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"> <p> </p> </body> </html>
파일명: view.html <?php ##### 서버푸쉬를 위한 헤더 함수 호출 header("Content-type:multipart/mixed");
print(" <html> <head><title>메일 전송 테스트</title> <link rel='stylesheet' href='./style.css' type='text/css'>
</head> <body class='mainText' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'> "); flush(stdout);
##### 공유 메모리 지정 $shm_key = 0x9999;
$chkTime = time();
$temp = "no";
while (1) { $shmid = shm_attach($shm_key,46084,0400); $message = shm_get_var ($shmid,1); // 공유메모리에 저장된 체팅 클래스를 가져옴 shm_detach ($shmid); $chkmessage = substr($message,0,7);
if ($temp != $message) { print (" $message<br> <script Language='javascript'> self.scroll(0,100000000); </script> "); $temp = $message; $chkTime = time(); }
if ($chkTime < time() - 120) { echo (" <script Language='javascript'> alert('2분 동안 아무도 메시지 입력이 없어 대화방이 닫힙니다.'); parent.location.replace('index.html'); </script> "); break; }
flush(stdout); usleep(100000); } shm_detach ($shmid); ?>
파일명: phpMessage.html <?php ##### 메시지를 공유메모리상의 클래스에 집어 넣는 프로그램 if ( $comment ) { $shm_key = 0x9999; $semid = sem_get($shm_key,10); sem_acquire($semid); $shmid = shm_attach($shm_key,46084);
$message = "<FONT SIZE='2' COLOR='BLACK'><B>".$name."</B> : ".$comment."</font>";
shm_put_var($shmid,1,$message); shm_detach ($shmid); sem_release($semid);
echo (" <script Language='javascript'> parent.down.document.form1.comment.value=''; parent.down.document.form1.comment.focus(); </script> "); exit; } ?>
|
[목차] |