Instant Messaging
Plainly, yes.
Establish connection/Receive message/Send Message
But there's a lot of scrutiny.
Here is a simple demo
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "UTF-8"> <Metaname= "Viewport"content= "initial-scale=1, maximum-scale=3, minimum-scale=1, User-scalable=no"> <title></title> <Linkrel= "stylesheet"href= "Css/base.css" /> <Scripttype= "Text/javascript"src= "Js/websocket.js"></Script> </Head> <Body> <Divclass= "Wrap"> <Divclass= "Sideleft"> <H1>Online personnel</H1> <ul> <Li> <Divclass= "Headimg"></Div> <span>18768875722</span> </Li> <Li> <Divclass= "Headimg"></Div> <span>15277365552</span> </Li> </ul> </Div> <Divclass= "Sideright"> <DivID= "Liao"> <Div> <Divclass= "Headimg"></Div> <Divclass= "AAA">Hello, Mr. B.</Div> </Div> <Divstyle= "float:right;"> <Divclass= "BBB"ID= "Me">Score and health</Div> <Divclass= "Headimg"></Div> </Div> </Div> <Divclass= "Connect"> <Div> <inputtype= "text"style= "height:40px;width:180px;"ID= "Content" /> </Div> <Div> <inputtype= "text"ID= "num"style= "position:absolute;bottom:10px;right:140px;" /> <Buttonclass= "DL"onclick= "Connect ()">Login</Button> <Buttonclass= "QL"onclick= "Send ()">Group Chat</Button> <Buttonclass= "SL">Private chat</Button> </Div> </Div> </Div> <!--<div id= "Logs" ></div> - </Div> <Script> </Script> </Body></HTML>
The interface is as follows
Here is the WebSocket code, can achieve the effect is, first login with the account, and then open Group chat or private chat
var Curuser, username, socket; function Connect () { = document.getElementById ("num"). Value; if (curuser) { alert ("Currently logged in, please log out first!") ); return ; }
Instead of the IP you need
varip = "106.00.0.000";
varPort = 8888; varPassword = "123"; Socket =NewWebSocket ("WS:" + IP + ":" + Port + "? username=" + Username + "&password=" +password); Socket.onopen=function(e) {Console.log ("Successful Landing"); varUsercmd = "{\" cmd\ ": 17,\" type\ ": \" 0\ ", \" userid\ ": \" + username + "\"} "; Socket.send (Usercmd); //get login user information; }; Socket.onerror=function(e) {Console.log ("Login Exception"); Console.log (E.message)}; Socket.onmessage=function(e) {vardata = eval ("(" +e.data+ ")"); varContentdata = eval ("(" +data.data+ ")"); //Receive Message triggering if(Data.command = = "Command_chat_resp" & contentdata.from! =username) {Console.log ("Received successfully"); document.getElementById ("Liao"). InnerHTML + = "<br/><div><div class= ' headimg ' ></div><div class= ' aaa ' >" + contentdata.content+ "</div></div>"; } }}varonselected;//sending MessagesfunctionSend () {varToid = ""; if(onselected) {toid= "1876887572210"; } varCreatetime =NewDate (). GetTime (); varContent = document.getElementById (' content '). Value; if(Content = = "") return;
Group Chat Codevarmsg = "{\" from\ ": \" "+username+" \ ", \" createtime\ ":" +createtime+ ", \" cmd\ ": 11,\" group_id\ ": \" 100\ ", \" chattype\ ": \" 1 \ ", \" msgtype\ ": \" text\ ", \" content\ ": \" "+content+" \ "}";
Private chat Code
varmsg = "{\" from\ ": \" "+ Username +" \ ", \" to\ ": \" "+ toid +" \ ", \" cmd\ ": 11,\" createtime\ ":" + createtime + ", \" msgtype\ ": \ "Text\", \ "content\": \ "" + Inputvalue + "\"} ",
socket.send (msg); var chatobj = eval ("(" + msg + ")"); Console.log (chatobj); // document.getElementById ("Me"). InnerHTML = chatobj.content; document.getElementById ("Liao"). InnerHTML + = "<br/><div style= ' float:right; ' ><div class= ' bbb ' id= ' me ' > ' +chatobj.content+ ' </div><div class= ' headimg ' ></div></div > "; var createtime = "2017-10-20"; document.getElementById (' content '). Value = "";}
Initial WebSocket