博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于html5 localStorage , web SQL, websocket的简单聊天程序
阅读量:7122 次
发布时间:2019-06-28

本文共 5007 字,大约阅读时间需要 16 分钟。

new function() {	var ws = null;	var connected = false;	var serverUrl;	var connectionStatus;	var sendMessage;		var connectButton;	var disconnectButton; 	var sendButton;	var open = function() {		var url = serverUrl.val();		ws = new WebSocket(url);		ws.onopen = onOpen;		ws.onclose = onClose;		ws.onmessage = onMessage;		ws.onerror = onError;		localStorage.setItem("server", url);		connectionStatus.text('OPENING ...');		serverUrl.attr('disabled', 'disabled');		connectButton.hide();		disconnectButton.show();	};		var close = function() {		if (ws) {			console.log('CLOSING ...');			ws.close();		}		connected = false;		connectionStatus.text('CLOSED');		serverUrl.removeAttr('disabled');		connectButton.show();		disconnectButton.hide();		sendMessage.attr('disabled', 'disabled');		sendButton.attr('disabled', 'disabled');		loginName.attr('disabled', 'disabled');		loginPass.attr('disabled', 'disabled');		loginButton.attr('disabled', 'disabled');	};		var clearLog = function() {		$('#messages').html('');	};		var login = function(event) {		var name = loginName.val();		var password = loginPass.val();		if(name!= "" && password != ""){			var msg= "login|"+ name + "|"+ password;			ws.send(msg);			localStorage.setItem("name", name);			localStorage.setItem("password", password);					}else{			alert("name and  password  cant't be empty!")		}	};	var doLogin= function(msg){			if(msg.substr(0, 5)== "login"){			sendMessage.removeAttr('disabled');			sendButton.removeAttr('disabled');			loginName.attr('disabled', 'disabled');			loginPass.attr('disabled', 'disabled');			loginButton.attr('disabled', 'disabled');			return "login success!"		}else if(msg.substr(0, 3)== "msg"){			return msg.substr(4)		}else{		}		return ""	};	var onOpen = function() {		console.log('OPENED: ' + serverUrl.val());		connected = true;		connectionStatus.text('OPENED');		//sendMessage.removeAttr('disabled');		//sendButton.removeAttr('disabled');		loginName.removeAttr('disabled');		loginPass.removeAttr('disabled');		loginButton.removeAttr('disabled');	};		var onClose = function() {		console.log('CLOSED: ' + serverUrl.val());		ws = null;	};		var onMessage = function(event) {		var data = event.data;		//login		data= doLogin(data)		//todo		if(data){			addMessage(data);			//$('#messages').val("");			//getLog();			db.transaction(function (tx) {  			  tx.executeSql('INSERT INTO LOGS (content) VALUES ("'+ data+ '")');			});		}	};		var onError = function(event) {		alert("error, maybe socket closed!")		//alert(event.data);	};		var addMessage = function(data, type) {		var msg = $('
').text(data);		if (type === 'SENT') {			msg.addClass('sent');		}		var messages = $('#messages');		messages.append(msg);		messages.append($("
")); var msgBox = messages.get(0); while (msgBox.childNodes.length > 10000) { msgBox.removeChild(msgBox.firstChild); } msgBox.scrollTop = msgBox.scrollHeight; }; var getLog= function(start, limit){ if(typeof(start) == undefined || !start){ start= 0 } if(typeof(limit) == undefined || !limit){ limit= 10000 } //log db.transaction(function (tx) { tx.executeSql('SELECT * FROM LOGS limit ?, ?', [start, limit], function (tx, results) { var len = results.rows.length, i; for (i = 0; i < len; i++){ msg = results.rows.item(i).content; //document.querySelector('#status').innerHTML += msg; //alert(msg) addMessage(msg); } }, null); }); }; WebSocketClient = { init: function() { serverUrl = $('#serverUrl'); connectionStatus = $('#connectionStatus'); sendMessage = $('#sendMessage'); connectButton = $('#connectButton'); disconnectButton = $('#disconnectButton'); sendButton = $('#sendButton'); loginName= $('#name'); loginPass= $('#password'); loginButton= $('#loginButton'); if(cache_server!= null)serverUrl.val(cache_server) if(cache_name!= null)loginName.val(cache_name) if(cache_password!= null)loginPass.val(cache_password) getLog(); loginButton.click(function(e) { login(); }); connectButton.click(function(e) { close(); open(); }); disconnectButton.click(function(e) { close(); }); sendButton.click(function(e) { var msg = $('#sendMessage').val(); if(msg== "" ){ alert("You must say something!") }else{ ws.send("msg|"+ msg); $('#sendMessage').val("") } }); $('#clearMessage').click(function(e) { clearLog(); }); var isCtrl; sendMessage.keyup(function (e) { if(e.which == 17) isCtrl=false; }).keydown(function (e) { if(e.which == 17) isCtrl=true; if(e.which == 13 && isCtrl == true) { sendButton.click(); return false; } }); } };}$(function(){ cache_server= localStorage.getItem("server") cache_name= localStorage.getItem("name") cache_password= localStorage.getItem("password") db = openDatabase('mydb', '1.0', 'Test DB', 5 * 1024 * 1024); db.transaction(function (tx){ tx.executeSql("CREATE TABLE IF NOT EXISTS LOGS ('id' integer primary key autoincrement,'content' text)"); }); WebSocketClient.init();});

转载地址:http://otxel.baihongyu.com/

你可能感兴趣的文章
wxPython之Socket客户端
查看>>
linux系统目录结构详解(简单易懂)
查看>>
学习《Effective C++》
查看>>
CS224n笔记9 机器翻译和高级LSTM及GRU
查看>>
KVM虚拟机
查看>>
GdiPlus[57]: 图像(九) IGPBitmap 特有的属性与方法
查看>>
Windows 多媒体函数(winmm.dll 中的函数)汇总
查看>>
关于 Delphi 中流的使用(4) 遍历读取流中的所有数据
查看>>
使用 IntraWeb (4) - 页面布局之 TIWRegion
查看>>
域控的升级及客户端加入域
查看>>
【Java每日一题】20161129
查看>>
[译文]greenlet:轻量级并发程序
查看>>
五分钟学会HTML
查看>>
请求Servlet 得到 Request 里所有对象
查看>>
volatile 和 synchronized 的比较
查看>>
Java递归
查看>>
windows 操作系统原版下载地址
查看>>
剑指offer——O(1)时间删除单链表节点
查看>>
OSPF在企业网络中的应用
查看>>
什么是NIO(转载)
查看>>