'use strict';varbtnConn=document.querySelector('button#connect');varbtnLeave=document.querySelector('button#leave');varbtnSend=document.querySelector('button#send');varuserNicknameInput=document.querySelector('input#user-nickname');varuserInput=document.querySelector('input#user-input');varmsgList=document.querySelector('ul#msg-list');varsocket=null;// 连接varuid=null;varnickname="RustFisher";// 昵称varnameList=["人参","卜芥","儿茶","八角","丁香","刀豆","三七","大蓟","山药","川乌","天冬","天麻","元胡"];varnameIndex=Math.floor(Math.random()*nameList.length);nickname=nameList[nameIndex];// 随机昵称userNicknameInput.value=nickname;// 获取浏览器指纹constfpPromise=import('https://openfpcdn.io/fingerprintjs/v3').then(FingerprintJS=>FingerprintJS.load())// Get the visitor identifier when you need it.fpPromise.then(fp=>fp.get()).then(result=>{// This is the visitor identifier:uid=result.visitorIdconstvisitorId=result.visitorIdconsole.log(visitorId)})functionaddMsgToList(type,msgBody){varli=document.createElement("li");li.setAttribute("id","newli");if(type==0){li.innerHTML=msgBody;}elseif(type==1){li.innerHTML=msgBody.nickname+": "+msgBody.msg;}msgList.appendChild(li);}// 发起连接functionconn(){if(uid==null){console.error('无法连接,还没获取到uid');return;}console.log('try connect');constwssUrl="http://localhost:3000";// localhost debugsocket=io(wssUrl,{path:'/chat1'});socket.on('connect',function(){console.log('连接成功');// 连接状态 连上addMsgToList(0,'连接成功');});socket.on('disconnect',(socket)=>{console.log('socket已断开',socket);// 连接状态 已断开socket=null;});socket.on('debug',(data)=>{console.log('[服务器消息][debug], ',data);});socket.on('allMsg',(data)=>{console.log('[服务器消息][allMsg], ',data);addMsgToList(1,data);});socket.emit('debug',{uid:uid,msg:"web连接"});returntrue;}functiondisconnect(){if(socket){socket.disconnect();}}functionsendMsg(){varinput=userInput.value;if(input==null||input.length==0){console.warn('请输入文本');return;}if(socket){socket.emit('m2s',{uid:uid,nickname:userNicknameInput.value,msg:input});}}btnConn.onclick=conn;btnLeave.onclick=disconnect;btnSend.onclick=sendMsg;