From cfb46db181612d42d0be02789db6203f7d9a6db0 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 27 Mar 2017 22:29:30 +0200 Subject: webclient: Basic messaging possible --- webclient/client.html | 178 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 150 insertions(+), 28 deletions(-) (limited to 'webclient') diff --git a/webclient/client.html b/webclient/client.html index 3472cb8..2305c17 100644 --- a/webclient/client.html +++ b/webclient/client.html @@ -7,13 +7,13 @@ var sock=null,username=null; var roomlist=[":console"]; var currentroom=":console"; -var roomlogs={":console":[]}; +var roomlogs=new Map([[":console",[]]]); -(function(){ +var uniqid=(function(){ var id=1; - function uniqid(){ + return function uniqid(){ return id++; - } + }; })(); var net_callbacks={}; @@ -24,6 +24,32 @@ function net_send(msg,cb){ net_callbacks[id]=cb; } +function fancysplit(text,freespace){ + var obj={ + word: [], + rest: [], + }; + var cursor=0,idx; + if(freespace){ + while(cursor=2){ @@ -99,7 +145,9 @@ function updateRoomList(){ if(roomlist[i]==currentroom)div.classList.add("selected"); div.addEventListener("click",function(roomid){ currentroom=roomid; + updateRoomList(); drawRoom(currentroom); + document.getElementById("roominput").focus(); }.bind(this,roomlist[i])); div.appendChild(document.createTextNode(roomlist[i])); rldiv.appendChild(div); @@ -117,7 +165,7 @@ function drawRoom(roomid){ else roomtitle.firstChild.nodeValue=roomid; var tr,td; - var items=roomlogs[roomid]; + var items=roomlogs.get(roomid); if(!items){ alert("drawRoom on nonexistent roomid '"+roomid+"'!"); return; @@ -130,17 +178,28 @@ function drawRoom(roomid){ function drawRoomEntry(type,args){ var tbody=document.getElementById("roomlog"); var tr=document.createElement("tr"); - var td; + var td1=document.createElement("td"),td2=document.createElement("td"); + var node1=document.createTextNode(""),node2=document.createTextNode(""); + td1.appendChild(node1); td2.appendChild(node2); + tr.appendChild(td1); tr.appendChild(td2); + tbody.appendChild(tr); switch(type){ case "message": - td=document.createElement("td"); - td.appendChild(document.createTextNode(args[0])); - tr.appendChild(td); - td=document.createElement("td"); - td.classList.add("message"); - td.appendChild(document.createTextNode(args[1])); - tr.appendChild(td); - tbody.appendChild(tr); + tr.classList.add("message"); + node1.nodeValue="<"+args[0]+">"; + node2.nodeValue=args[1]; + break; + + case "error": + tr.classList.add("error"); + node1.nodeValue="--"; + node2.nodeValue=args[0]; + break; + + case "notice": + tr.classList.add("notice"); + node1.nodeValue="--"; + node2.nodeValue=args[0]; break; default: @@ -150,21 +209,73 @@ function drawRoomEntry(type,args){ } function addRoomEntry(roomid,type,args){ - if(!roomlogs[roomid]){ + if(!roomlogs.get(roomid)){ alert("addRoomEntry on nonexistent roomid '"+roomid+"'!"); return; } - roomlogs[roomid].push([type,args]); + roomlogs.get(roomid).push([type,args]); if(roomid==currentroom){ drawRoomEntry(type,args); } } +function executeCommand(roomid,text){ + var spl=fancysplit(text,true); + if(spl.word.length==0)return; + var cmd=spl.word[0]; + var creds=null; + switch(cmd){ + case "register": + case "login": + if(spl.word.length<3){ + addRoomEntry(roomid,"error",["Usage: /login "]); + break; + } + creds=[spl.word[1],spl.rest[2]]; + net_send(cmd+" "+creds[0]+" "+creds[1],function(ok,err){ + if(ok){ + username=creds[0]; + addRoomEntry(roomid,"notice",["Logged in as user '"+creds[0]+"'"]); + updateStatus(); + fetchRoomList(); + } else { + addRoomEntry(roomid,"error",["Unable to "+cmd+": "+err]); + } + }); + break; + + default: + addRoomEntry(roomid,"error",["Unknown command '"+cmd+"'"]); + break; + } +} + +function sendMessage(roomid,text){ + if(roomid[0]==":"){ + addRoomEntry(roomid,"error",["Cannot send a message here"]); + return; + } + var sentAs=username,msg=text.replace(/\n/g,""); + net_send("send "+roomid+" "+msg,function(ok,err){ + if(ok){ + addRoomEntry(roomid,"message",[sentAs,msg]); + return; + } + addRoomEntry(roomid,"error",["Unable to send message: "+err]); + }); +} + function doSend(){ var inputelem=document.getElementById("roominput"); var text=inputelem.value; inputelem.value=""; - addRoomEntry(currentroom,"message",["-",text]); + + if(text.length==0)return; + if(text[0]=="/"){ + executeCommand(currentroom,text.slice(1)); + } else { + sendMessage(currentroom,text); + } } function doKeypress(ev){ @@ -174,6 +285,7 @@ function doKeypress(ev){ window.addEventListener("load",function(){ reconnect(); drawRoom(currentroom); + document.getElementById("roominput").focus(); });