基于html5 localStorage , web SQL, websocket的简单聊天程序
程序员文章站
2022-08-11 11:16:43
new function() {
var ws = null;
var connected = false;
var serverUrl;
var connectionS...
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
上一篇: php 获取时间的方法
下一篇: python快速排序介绍(实例)