The user name used by the Var username,//user when they log in to the Socket.io chat room socket,//socket Port Object connected to the chat server tbxusername,//User Name text box tbxmsg,//Dialog text Box Display the message area in the divchat;//page function Window_onload () { Divchat=document.getelementbyid ("Divchat"); Tbxusername=document.getelementbyid ("Tbxusername"); Tbxmsg=document.getelementbyid ("tbxmsg"); Tbxusername.focus (); Tbxusername.select (); } function Addmsg (msg) { divchat.innerhtml+=msg+ "<br>"; if (divchat.scrollheight>divchat.clientheight) Divchat.scrolltop=divchat.scrollheight-divchat.clientheight; } function Btnlogin_onclick () { if (tbxUsername.value.trim () = = "") { Alert ("Please enter user name"); Return } Username=tbxusername.value.trim (); Socket=io.connect (); Socket.on ("Connect", function () { Addmsg ("Connection to chat server has been established."); Socket.on ("Login", function (name) { Addmsg ("Welcome to the user" +name+ "into the chat room."); }); Socket.on ("Sendclients", function (names) { var Divright=document.getelementbyid ("Divright"); var str= ""; Names.foreach (function (name) { str+=name+ "<br/>"; }); divright.innerhtml= "user list <br/>"; DIVRIGHT.INNERHTML+=STR; }); Socket.on ("Chat", function (data) { Addmsg (data.user+ "said:" +data.msg); }); Socket.on ("Disconnect", function () { Addmsg ("The connection to the chat server is disconnected."); document.getElementById ("Btnsend"). Diabled=true; document.getElementById ("Btnlogout"). Disabled=true; document.getElementById ("Btnlogin"). disabled= ""; var Divright=document.getelementbyid ("Divright"); divright.innerhtml= "User list"; }); Socket.on ("Logout", function (name) { Addmsg ("User" +name+ "has exited the chat room.") }); Socket.on ("Duplicate", function () { Alert ("The user name is already in use."); document.getElementById ("Btnsend"). Disabled=true; document.getElementById ("Btnlogout"). Disabled=true; document.getElementById ("Btnlogin"). disabled= ""; }); }); socket.on ("Error", function (err) { addmsg (" There was an error with the link between the chat server. "); socket.disconnect (); socket.removealllisteners ("Connect"); io.sockets={}; }); socket.emit ("login", userName); document.getElementById ("Btnsend"). disabled= ""; document.getElementById ("Btnlogout"). disabled= ""; document.getElementById ("Btnlogin"). Disabled=true; } function Btnsend_onclick () { var Msg=tbxmsg.value; if (msg.length>0) { Socket.emit ("chat", {user:username,msg:msg}); Tbxmsg.value= ""; } } function Btnlogout_onclick () { Socket.emit ("logout", userName); Socket.disconnect (); Socket.removealllisteners ("Connect"); io.sockets={}; Addmsg ("User" +username+ "Exit chat Room:"); var Divright=document.getelementbyid ("Divright"); divright.innerhtml= "User list"; document.getElementById ("Btnsend"). disabled= "Disabled"; document.getElementById ("Btnlogout"). disabled= "Disabled"; document.getElementById ("Btnlogin"). disabled= ""; } |