⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chat.php

📁 ajax开发的聊天室,无刷新技术实现,用于开发在线聊天室.
💻 PHP
📖 第 1 页 / 共 2 页
字号:
		this.resetData();
	};

	this.createAJAX = function(){
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e1){
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}
		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value) {
		var first = (this.URLString.length == 0);
		this.URLString += (first)?"":"&";
		this.URLString += name + "=" + this.encode(value);
	};

	this.send = function() {
		if (!this.xmlhttp || this.failed ) {
			this.onError();
			return;
		}
		var self = this;
		if (this.method == "GET" || this.method == "GET&POST") {
			this.xmlhttp.open(this.method,this.file+"?"+this.URLString,this.late);
		} else if (this.method == "POST") {
			this.xmlhttp.open(this.method,this.file,this.late);
			try {
				this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			} catch(e) {}
		}
		else this.onError();

		this.xmlhttp.onreadystatechange = function() {
			switch (self.xmlhttp.readyState) {
				case 1:
					self.onLoading();
					break;
				case 2:
					self.onLoaded();
					break;
				case 3:
					self.onInteractive();
					break;
				case 4:
					self.response = self.xmlhttp.responseText;
					self.responseXML = self.xmlhttp.responseXML;
					if (self.xmlhttp.status == "200") {
						self.onCompletion();
					} else {
						self.onError();
					}
					self.URLString = "";
					break;
			}
		};
		
		if (this.method == "POST") {
			this.xmlhttp.send(this.URLString);
		} else if (this.method == "GET") {
			this.xmlhttp.send(null);
		} else {
			this.xmlhttp.send(this.content);
		}
	};

	this.reset();
	this.createAJAX();
}

function pickColor()
{
	var sColor = $('dlgHelper').ChooseColorDlg();
	var color = sColor.toString(16);
	while (color.length<6) color="0"+color;
	window.color = color;
	color = "#"+color;
	$('div_color').style.backgroundColor = color;
	$('div_color').value = color;
}

var isIE = (document.all && window.ActiveXObject) ? true : false;
</script>
</head>
<body>
<center>
<div class=mydiv style='text-align:center; border:0px; background-color:transparent; font-size:25px; color:#ff8c05;'><?php echo $title;?></div>

<div class="mydiv login" id='div_description'>
<?php echo $description;?>
</div>

<div class="mydiv rooms" id='div_msg'>
<div class='contents' style='height:350px;' id='div_contents'>Loading...</div>
</div>

<div class="mydiv login" id='div_name' style='display:block;'>
Name:<input type=text class="inputtext bg" size=8 id='chat_user' value='' />&nbsp;
<OBJECT id=dlgHelper CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" WIDTH="0px" HEIGHT="0px"></OBJECT>
<input class="inputtext" style='width:50px;cursor:hand;10px;background-color:#000000;color:#ffffff;' id='div_color' onclick="pickColor()" value="#000000" onblur="this.style.backgroundColor=this.value;window.color=this.value.replace('#','');" />
&nbsp;Size:<input class="inputtext bg" type=text style='width:20px' maxlength=3 id='input_size' value='16' />(px)
&nbsp;Font:<select id='input_font' class='inputtext bg' style='width:70px;'>
<option value='Fixedsys'>Fixedsys</option>
<option value='heiti'>黑体</option>
<option value='songti'>宋体</option>
<option value='kaiti'>楷体</option>
</select>
Bold:<input type=checkbox id='input_bold' class='inputtext' style='border-bottom:0px;' />
Window:<a class='mybut' href='javascript:resize(1)'>+</a>
&nbsp;<a class='mybut' href='javascript:resize(0)'>-</a>
&nbsp;<a class='mybut' style='width:25px;font-size:16px;' href='javascript:clearAll()'>Clear</a>
</div>
<div class="mydiv login" id='div_word'>
<textarea type=text class="inputtext bg" rows=1 scrolling=no style='height:20px;overflow:hidden;width:500px;' id='chat_word' onfocus="if (this.value == '<?php echo $lang["hereyourwords"];?>') this.value='';" 
 onkeydown="return check_send(event);" ><?php echo $lang["hereyourwords"];?></textarea>
<input type=button class=submit value='Send' onclick="chat_send();$('chat_word').value='';$('chat_word').style.height=20;" onfocus="this.blur();"/>
</div>

<div class='mydiv' style='display:<?php if (!$disonline) echo "none";?>' id='div_online'>Loading online...</div>

<script>
var lastmod = <?php echo time()-$earlier*60;?>;
var login = 1;
var loading = false;
var olduser = getCookie('chatusername');
if (olduser != "") $('chat_user').value = olduser;
var room = "<?php echo $room;?>";
var first = 1;
var dis = "<?php echo $least;?>";
var lastword;
var color='';
var touchs = <?php echo $touchs;?>;
var dotouch = true;
var maxdisplay = <?php echo $maxdisplay;?>;
var nowdisplay = 1;

function load_word(re)
{
	if (re) setTimeout("load_word(1);",1000);
	if (window.loading) return;
	var sk = new sack("");
	sk.method = "GET&POST";
	sk.setVar("action","read");
	sk.setVar("lastmod",window.lastmod);
	sk.setVar("room",room);
	if (window.first)
	{
		sk.setVar("first","true");
		sk.setVar("dis",dis);
	}
	if (window.dotouch) 
	{
		sk.setVar("touchme","true");
		window.dotouch = false;
		try { CollectGarbage(); } catch(e) {}
	}
	sk.onCompletion = function()
	{
		window.loading = false;
		var body = $('div_contents');
		try {
			//alert(sk.response);
			eval("var arr = "+sk.response); 
		} catch(e)
		{
			alert('Error 101\nJSON syntax error!\n\n'+sk.response);
			return;
		}
		if (!arr || !arr.lastmod)
		{
			return;
		}
		var html = "";
		var line = arr.lines;
		var i = 0;
		var v1 = 0;
		var div_online = $('div_online');
		if (window.first)
		{
			body.innerHTML = "";
			window.first = false;
		}
		
		if (arr.onlines)
		{
			$('div_online').innerHTML = "";
			for(var i=0;i<arr.onlines.length;i++) addonline(arr.onlines[i]);
		}

		for(var i=0;i<line.length;i++)
		{
			var div1 = document.createElement("div");
			window.nowdisplay ++;
			if (window.nowdisplay > window.maxdisplay) window.nowdisplay = 1;
			if ($("contentitem"+window.nowdisplay)) body.removeChild($("contentitem"+window.nowdisplay));
			div1.className = "content";
			div1.id = "contentitem"+window.nowdisplay;
			div1.innerHTML = line[i].word+" <span class='time'>("+line[i].time+")</span>";
			body.appendChild(div1);
			body.scrollTop = 655350;
			v1 = 1;
		}
		if (v1) 
		{
			window.focus(); 
			document.body.focus();
			window.lastmod = arr.lastmod;
			$('chat_word').focus();
		}
	}

	sk.onError=function()
	{
		window.loading = false;
		window.status = 'Error 102';
		setTimeout("window.status = '';",5000);
	}
	window.loading = true;
	sk.content = $('chat_user').value;
	sk.send();
}

function touchme()
{
	window.dotouch = true;
	setTimeout("touchme()",window.touchs*1000);
}

function showalert(a,n)
{
	if (!n) n=0;
	if (n>3) return;
	if (!a)
	{
		a = 0;
		b = 1;
	}
	else
	{
		a = 1;
		b = 0;
	}
	document.title = mytitle[a];
	setTimeout("showalert("+b+","+(n+1)+");",500);
}

function addonline(name)
{
	if ($(name)) return;
	var d1 = document.createElement("div");
	d1.id = name;
	d1.innerHTML = name;
	d1.className = "online";
	$('div_online').appendChild(d1);
}

load_word(1);
touchme();

function check_send(e)
{
	if (!e) e = window.event;
	var obj = $('chat_word');
	if (isIE) obj.style.height = obj.scrollHeight+3;
	if (e.keyCode == 13)
	{
		if ((!e.shiftKey && !e.altKey && !e.ctrlKey) || !isIE)
		{
			chat_send();
			obj.value="";
			obj.style.height = 20;
			return false;
		}
		else if (isIE) obj.style.height = obj.scrollHeight+18;
	}
	return true;
}

function chat_send(n)
{
	if (!n) n=0;
	var username = $('chat_user').value.replace("\n","");
	var content = $('chat_word').value;
	var size = parseInt($('input_size').value);
	var font = $('input_font').value;
	var bold = ($('input_bold').checked)?"bold":"";
	if (username == "")
	{
		alert('Please enter your nick name first!!');
		$('chat_user').focus();
		return;
	}
	if (content == "" || content == "\n" || content == "\n\n" || content == "\n\n\n")
	{
		alert('Please enter your words!');
		$('chat_word').focus();
		$('chat_word').value = "";
		return;
	}
	if (size>100) size = 100;
	else if (size<0) size = 1;
	lastword = content;
	var sk = new sack("");
	sk.method = "GET&POST";
	sk.setVar("action","write");
	sk.setVar("bold",bold);
	sk.setVar("color",window.color);
	sk.setVar("font",font);
	sk.setVar("size",size);
	sk.setVar("room",room);
	sk.onCompletion = function()
	{
		//alert(sk.response);
		if (sk.response.indexOf("NAME")!=-1)
		{
			alert('昵称重复!');
			$('chat_user').value = "";
			$('chat_user').focus();
		}
		else if (sk.response.indexOf("repeat")!=-1)
		{
			$('chat_word').value = sk.lastcontent;
		}
		if (!window.loading)
		{
			window.dotouch = true;
			load_word();
		}
	}
	
	sk.onError=function()
	{
		alert('Error 103\nwhen send words\n\nYou can send them again!');
		$('chat_word').value = lastword;
	}
	sk.content = username+"\n"+content;
	sk.send();
	sk.lastcontent = content;
	$('chat_word').focus();
	setCookie("chatusername",$('chat_user').value);
}

function resize(s)
{
	var o = $('div_contents').style;
	var h = parseInt(o.height);
	h = (s)?h+50:h-50;
	if (h<=50 || h>=3000) return;
	o.height = h;
	$('div_contents').scrollTop = 655350;
}

function clearAll()
{
	$('div_contents').innerHTML = "";
}
</script>
</center>
</body>
</html>
<?php
}
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -