当我还在学习Web开发时,我曾经做过一个简单的聊天脚本。您可以使用以下 javascript 函数来制作您的小聊天信使:
每 1 秒后通过 ajax 从 db 获取消息
function fetchMessage()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
flag_loading=1;
document.getElementById("chat").innerHTML=xmlhttp.responseText;
var objDiv = document.getElementById("chat");
objDiv.scrollTop = objDiv.scrollHeight;
}
/*else if (flag_loading=0)
{
document.getElementById("chat").innerHTML="loading...";
}*/
}
xmlhttp.open("GET", "fetchmessage.php", true);
xmlhttp.send();
}
setInterval("fetchMessage()",1000);
服务器端脚本 fetchmessage.php 放在这里
<?php
session_start();
include('connect.php');
$user_id=@$_GET['user_id'];
$sql="select m.user_name,m.message,m.post_time from message m where m.user_id='".$_SESSION['current_user']['user_id']."' or m.reciever_id='".$_SESSION['current_user']['user_id']."' ";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "<br><font size=2 color='brown'><b>".$row['user_name'].":</b></font> ".$row['message']."</br>";
echo "<font size=1 color='blue'>(".$row['post_time'].")</font>";
}
?>
此功能将您的消息发送给其他用户(与您聊天)
function addMessage(reciever_id, user_id, message) {
if (message=='') {
window.alert("Enter some message.");
} else if (reciever_id=='none') {
window.alert("Select any friend.");
} else {
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
document.getElementById('message').value="";
}
}
xmlhttp.open("GET", "addmessage.php?user_id="+user_id+"&message="+message+"&reciever_id="+reciever_id, true);
xmlhttp.send();
}
}
服务器端脚本 addmessage.php 放在这里
<?php
session_start();
include('connect.php');
if (!isset($_SESSION['current_user'])) {
header("location:index.php?error=Please Login First!");
}
else {
$user_id=$_GET['user_id'];
$message=$_GET['message'];
$reciever_id=$_GET['reciever_id'];
mysql_query("INSERT INTO message VALUES (NULL, '".$_SESSION['current_user']['user_id']."', '".$reciever_id."', '".$_SESSION['current_user']['user_name']."', '".$message."', NOW())") or die(mysql_error());
}
?>
您还可以使用此脚本检查在线用户/朋友
function checkOnline() {
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('online_friends').innerHTML=xmlhttp.responseText;
//window.alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "onlinefriends.php", true);
xmlhttp.send();
}
setInterval("checkOnline()", 1000);
onlinefriends.php 的服务器端脚本放在这里
<?php
session_start();
include('connect.php');
$query_online=mysql_query("SELECT user_name FROM user WHERE user_status=1 AND user_name!='".$_SESSION['current_user']['user_name']."' ") or die(mysql_error());
while ($row=mysql_fetch_array($query_online)) {
echo "<font size=4 style='font-style:italic; font-family:Georgia, 'Times New Roman', Times, serif'>".$row[0]."</font><br>";
}
?>
并问我您是否觉得难以实施,我会进一步帮助您:)