Sajax ÀÀ¿ë : äÆÃ
ÃÑ ÆäÀÌÁö ¼ö : 3121

Àüü ÇÔ¼ö/¿ë¾î»çÀü
ÇöÀçÀ§Ä¡ : ¹Ì´Ï»çÀÌÆ®>Ajax>Document>Sajax2


¼Ò°³

À̹ø¿¡´Â Sajax¸¦ ÀÌ¿ëÇØ¼­ Á» ´õ °í±Þ½º·¯¿î AjaxÀÀ¿ëÀ» ¸¸µé¾î º¸°íÀÚ ÇÑ´Ù. À̱ÛÀº http://www.modernmethod.com/sajax/examples.phtml À» Âü°íÇØ¼­ ÀÛ¼ºµÇ¾ú´Ù.

¾ÆÀ̵ð¾î

ÀÌ À¥Ã¤ÆÃ ÀÀ¿ëÀº ´ÙÀ½°ú °°Àº È帧À» °¡Áø´Ù.
  1. Client ¿¡¼­ Chat.php¸¦ ¿äûÇϸé, Sajax·Î ¸¸µé¾îÁø Javascript°¡ HTML ¹®¼­¿Í ÇÔ²² Client·Î Àü¼ÛµÈ´Ù.
  2. ³»¿ëÀ» ÀÔ·ÂÇϰí Àü¼Û¹öưÀ» ´©¸£¸é Chat.php·Î POST¹æ½ÄÀ¸·Î Àü´ÞµÇ°í
  3. Chat.php´Â Àü´ÞµÈ ³»¿ëÀ» ÆÄÀÏ¿¡ ¾´´Ù.
  4. Ŭ¶óÀ̾ðÆ®ÃøÀÇ Sajax javascript´Â 1Ãʰ£°ÝÀ¸·Î Data.php¿¡ »õ·Î¿î µ¥ÀÌÅͰ¡ ÀÖ´ÂÁö¸¦ ¿äûÇϰí
  5. »õ·Î¿î µ¥ÀÌÅͰ¡ ÀÖ´Ù¸é, Chat.php¸¦ ÅëÇØ¼­ ÃÖ±Ù 10°³ÀÇ µ¥ÀÌÅ͸¦ ¾ò¾î¿Â´Ù.
  6. ¾ò¾î¿Â µ¥ÀÌÅÍ´Â <div id="comment"> ¿¡ Ãâ·Â½ÃŲ´Ù.

´ÙÀ½Àº Chat.phpÀÇ ÄÚµå´Ù
<?php 
    // 
    // The world's least efficient wall implementation 
    // 
    require("../sajax-0.12/Sajax.php"); 
 
    function colorify_ip($ip) 
    { 
        $parts = explode(".", $ip); 
        $color = sprintf("%02s", dechex($parts[1])) . 
                 sprintf("%02s", dechex($parts[2])) . 
                 sprintf("%02s", dechex($parts[3])); 
        return $color; 
    } 
 
    function add_line($msg) { 
        $f = fopen("./tmp/wall.html", "a"); 
        $dt = date("Y-m-d h:i:s"); 
        $msg = rawurldecode($msg); 
        $msg = strip_tags(stripslashes($msg)); 
        $remote = $_SERVER["REMOTE_ADDR"]; 
        // generate unique-ish color for IP 
        $color = colorify_ip($remote); 
        fwrite($f, "<span style=\"color:#$color\">$dt</span> $msg<br>\n"); 
        fclose($f); 
    } 
 
    function refresh() { 
        $lines = file("./tmp/wall.html"); 
        // return the last 25 lines 
        return join("\n", array_slice($lines, -25)); 
    } 
 
    $sajax_request_type = "POST"; 
    sajax_init(); 
    sajax_export("add_line", "refresh"); 
    sajax_handle_client_request(); 
?> 
<html> 
<head> 
    <title>Wall</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=euc-kr" /> 
<style> 
div.comment 
{ 
    border:1px solid #000000; 
    padding:2px; 
    width:auto; 
} 
</style> 
    <script> 
    <? 
    sajax_show_javascript(); 
    ?> 
     
    var check_n = 0; 
    var old_data = "--"; 
     
    function refresh_cb(new_data) {  
        if (new_data != old_data) { 
            document.getElementById("comment").innerHTML = new_data; 
            setTimeout("refresh()", 1000); 
            old_data = new_data; 
        } else { 
            setTimeout("refresh()", 2500); 
        } 
        document.getElementById("status").innerHTML = "Checked #" + check_n++; 
    } 
     
    function refresh() { 
        document.getElementById("status").innerHTML = "Checking.."; 
        x_refresh(refresh_cb); 
    } 
     
    function add_cb() { 
        // we don't care.. 
    } 
 
    function add() { 
        var handle; 
        var r =''; 
        var s; 
        var i; 
 
        s = document.getElementById("line"); 
        // UniCode ó¸® 
        for(i=0; i < s.value.length; i++) {  
            if (s.value.charCodeAt(i) > 128) {  
                r += '&#'+s.value.charCodeAt(i)+';';  
            } else {  
                r += s.value.substr(i,1);  
            }  
        }  
        line = r; 
 
        r =''; 
        handle = document.getElementById("handle"); 
        for(i=0; i < handle.value.length; i++) {  
            if (handle.value.charCodeAt(i) > 128) {  
                r += '&#'+handle.value.charCodeAt(i)+';';  
            } else {  
                r += handle.value.substr(i,1);  
            }  
        }  
        handle = r; 
 
        if (line == "")  
            return; 
        sajax_request_type = "POST"; 
        x_add_line("[" + handle + "] " + line, add_cb); 
        document.getElementById("line").value = ""; 
    } 
    </script> 
     
</head> 
<body onload="refresh();" bgcolor=#FFFFFF> 
<div id='wikiBody'> 
<form name="f" action="#" onsubmit="add();return false;"> 
    <input type="text" name="handle" id="handle" value="(name)" 
        onfocus="this.select()" style="width:130px;"> 
    <input type="text" name="line" id="line" value="(¸Þ½ÃÁöÀÔ·Â)" 
        onfocus="this.select()" 
        style="width:300px;"> 
    <input type="button" name="check" value="Post message" 
        onclick="add(); return false;"> 
    <div id="comment"></div> 
    <div id="status"><em>Loading..</em></div> 
</form> 
</div> 
</body> 
</html>