Simple chat program based on HTML5 localstorage, Web SQL, WebSocket

Source: Internet
Author: User

New function () {var ws = Null;var connected = False;var Serverurl;var connectionstatus;var sendmessage;var connectbutton;v AR Disconnectbutton; var Sendbutton;var open = function () {var url = serverurl.val (); ws = new WebSocket (URL); ws.onopen = Onopen;ws.onclose = On Close;ws.onmessage = Onmessage;ws.onerror = Onerror;localstorage.setitem ("server", URL); Connectionstatus.text (' OPENING, serverurl.attr (' disabled ', ' disabled '), Connectbutton.hide ();d isconnectbutton.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;//logindata= dologin (data)//todoif (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 = $ (' <pre> '). text (data); if (type = = = ' SENT ') {msg.addclass (' SENT ');} var messages = $ (' #messages '); Messages.append (msg); Messages.append ($ ("<br>")), 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 | |!lim It) {limit= 10000}//logdb.transaction (tx) {tx.executesql (' SELECT * from LOGS limit?,? ', [Start, limit], functio N (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 ');d Isconnectbutton = $ (' #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 = =) Isctrl=false;}).  KeyDown (function (e) {if (E.which = =) Isctrl=true;if (E.which = = && 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 * 1024x768 * 1024x768);d b.transaction (function (TX) {Tx.executesql ("CREATE TABLE IF not EXISTS LOGS (' id ' integer primary key autoincrement, ' content ' text) ');}); Websocketclient.init ();});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.