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

📄 global.js

📁 ajax+visalsdio2005实现的聊天室程序
💻 JS
字号:
ChatRoom = new function()
{
	this._serviceUrl = "ChatService.ashx";
	this._getUserListInterval = 5000; // 5 seconds
	this._getMessagesInterval = 1000; // 2 seconds
	this._updateInterval = 300000; // 5 minutes
	
	this._createDelegate = function(instance, method)
	{
		return function()
		{
			method.apply(instance, arguments);
		}
	}
	
	this._ajaxUpdate = function(params, elementId, callback)
	{
		new Ajax.Updater(
			elementId,
			this._serviceUrl,
			{
				method: "post",
				evalScript: true,
				parameters: params,
				onFailure: callback,
				onException: callback,
				onComplete: callback
			});
	}
	
	this._ajaxRequest = function(params, callback)
	{
		new Ajax.Request(
			this._serviceUrl,
			{
				method: 'post',
				evalScript: true,
				parameters: params,
				onFailure: callback,
				onException: callback,
				onComplete: callback
			});
	}
	
	this.getUserList = function()
	{
		this._ajaxUpdate(
			"action=users",
			"UserList",
			this._createDelegate(this, this._getUserListCallback));
	}
	this._getUserListCallback = function()
	{
		setTimeout(
			this._createDelegate(this, this.getUserList),
			this._getUserListInterval);
	}
	
	this.getMessages = function()
	{
		this._ajaxUpdate(
			"action=get",
			"ChatContent",
			this._createDelegate(this, this._getMessagesCallback));
	}
	this._getMessagesCallback = function()
	{
		var ele = $("ChatContent");
		ele.scrollTop = ele.scrollHeight;
		setTimeout(
			this._createDelegate(this, this.getMessages),
			this._getMessagesInterval);
	}	
	
	this.update = function()
	{
		this._ajaxRequest(
			"action=update",
			this._createDelegate(this, this._updateCallback));
	}
	this._updateCallback = function()
	{
		setTimeout(
			this._createDelegate(this, this.update),
			this._updateInterval);
	}
	
	
	this.sendMessage = function()
	{
		var message = $("UserInputTextBox").value;
		if (message)
		{
			$("UserInputTextBox").value = "";
	
			var encodedMsg = encodeURI(message);
			this._ajaxRequest(
				"action=send&msg=" + encodedMsg);
		}
	}
	
	this.inputBox_KeyPress = function(e)
	{
		if (e.keyCode == 13)
		{
			this.sendMessage();
		
			try
			{
				e.preventDefault();
			}
			catch(e){}
			
			return false;
		}
		
		return true;		
	}
}

⌨️ 快捷键说明

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