var username ; var users ; var rid ; var base = 1000 ; var increase = 25 ; var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/ ; var LOGIN_ERROR = "There is no server to log in, please wait." ; var LENGTH_ERROR = "Name/Channel is too long or too short. 20 character max." ; var NAME_ERROR = "Bad character in Name/Channel. Can only have letters, numbers, Chinese characters, and '_'" ; var DUPLICATE_ERROR = "Please change your name to login." ; util = { urlRE : /https?:\/\/([-\w\.]+)+(:\d+)?(\/([^\s]*(\?\S+)?)?)?/g , toStaticHTML : function ( inputHtml ) { inputHtml = inputHtml . toString ( ) ; return inputHtml . replace ( /&/g , "&amp;" ) . replace ( /</g , "&lt;" ) . replace ( />/g , "&gt;" ) ; } , zeroPad : function ( digits , n ) { n = n . toString ( ) ; while ( n . length < digits ) n = '0' + n ; return n ; } , timeString : function ( date ) { var minutes = date . getMinutes ( ) . toString ( ) ; var hours = date . getHours ( ) . toString ( ) ; return this . zeroPad ( 2 , hours ) + ":" + this . zeroPad ( 2 , minutes ) ; } , isBlank : function ( text ) { var blank = /^\s*$/ ; return ( text . match ( blank ) !== null ) ; } } ; function scrollDown ( base ) { window . scrollTo ( 0 , base ) ; $ ( "#entry" ) . focus ( ) ; } ; function addMessage ( from , target , text , time ) { var name = ( target == '*' ? 'all' : target ) ; if ( text === null ) return ; if ( time == null ) { time = new Date ( ) ; } else if ( ( time instanceof Date ) === false ) { time = new Date ( time ) ; } var messageElement = $ ( document . createElement ( "table" ) ) ; messageElement . addClass ( "message" ) ; text = util . toStaticHTML ( text ) ; var content = '<tr>' + '  <td class="date">' + util . timeString ( time ) + '</td>' + '  <td class="nick">' + util . toStaticHTML ( from ) + ' says to ' + name + ': ' + '</td>' + '  <td class="msg-text">' + text + '</td>' + '</tr>' ; messageElement . html ( content ) ; $ ( "#chatHistory" ) . append ( messageElement ) ; base += increase ; scrollDown ( base ) ; } ; function tip ( type , name ) { var tip , title ; switch ( type ) { case 'online' : tip = name + ' is online now.' ; title = 'Online Notify' ; break ; case 'offline' : tip = name + ' is offline now.' ; title = 'Offline Notify' ; break ; case 'message' : tip = name + ' is saying now.' ; title = 'Message Notify' ; break ; } var pop = new Pop ( title , tip ) ; } ; param { enet . IDecodePackage } data function initUserList ( data ) { users = data . data ; for ( var i = 0 ; i < users . length ; i ++ ) { var slElement = $ ( document . createElement ( "option" ) ) ; slElement . attr ( "value" , users [ i ] ) ; slElement . text ( users [ i ] ) ; $ ( "#usersList" ) . append ( slElement ) ; } } ; function addUser ( user ) { var slElement = $ ( document . createElement ( "option" ) ) ; slElement . attr ( "value" , user ) ; slElement . text ( user ) ; $ ( "#usersList" ) . append ( slElement ) ; } ; function removeUser ( user ) { $ ( "#usersList option" ) . each ( function ( ) { if ( $ ( this ) . val ( ) === user ) $ ( this ) . remove ( ) ; } ) ; } ; function setName ( ) { $ ( "#name" ) . text ( username ) ; } ; function setRoom ( ) { $ ( "#room" ) . text ( rid ) ; } ; function showError ( content ) { $ ( "#loginError" ) . text ( content ) ; $ ( "#loginError" ) . show ( ) ; } ; function showLogin ( ) { $ ( "#loginView" ) . show ( ) ; $ ( "#chatHistory" ) . hide ( ) ; $ ( "#toolbar" ) . hide ( ) ; $ ( "#loginError" ) . hide ( ) ; $ ( "#loginUser" ) . focus ( ) ; } ; function showChat ( ) { $ ( "#loginView" ) . hide ( ) ; $ ( "#loginError" ) . hide ( ) ; $ ( "#toolbar" ) . show ( ) ; $ ( "entry" ) . focus ( ) ; scrollDown ( base ) ; } ; class NetEventHandler extends enet . DefaultNetEventHandler { } type { enet . NetNode } let netNode ; netNode = new enet . NetNode ( ) ; const protoHandler = new enetPinusPb . PinusProtoHandler ( ) ; const netEventHandler = new NetEventHandler ( ) ; netNode . init ( { protoHandler : protoHandler , netEventHandler : netEventHandler } ) ; function queryEntry ( uid , callback ) { var route = 'gate.gateHandler.queryEntry' ; netNode . connect ( { host : window . location . hostname , port : 3014 , handShakeReq : { } } , function ( ) { netNode . request ( route , { uid : uid } , function ( dpkg ) { netNode . disConnect ( ) ; if ( dpkg . code === 500 ) { showError ( LOGIN_ERROR ) ; return ; } callback ( dpkg . data . host , dpkg . data . port ) ; } ) ; } ) ; } ; $ ( document ) . ready ( function ( ) { showLogin ( ) ; netNode . onPush ( "onChat" , function ( dpkg ) { addMessage ( dpkg . data . from , dpkg . data . target , dpkg . data . msg ) ; $ ( "#chatHistory" ) . show ( ) ; if ( dpkg . data . from !== username ) tip ( 'message' , dpkg . data . from ) ; } ) ; netNode . onPush ( "onAdd" , function ( dpkg ) { var user = dpkg . data . user ; tip ( 'online' , user ) ; addUser ( user ) ; } ) ; netNode . onPush ( "onLeave" , function ( dpkg ) { var user = dpkg . data . user ; tip ( 'offline' , user ) ; removeUser ( user ) ; } ) ; $ ( "#login" ) . click ( function ( ) { username = $ ( "#loginUser" ) . attr ( "value" ) ; rid = $ ( '#channelList' ) . val ( ) ; if ( username . length > 20 || username . length == 0 || rid . length > 20 || rid . length == 0 ) { showError ( LENGTH_ERROR ) ; return false ; } if ( ! reg . test ( username ) || ! reg . test ( rid ) ) { showError ( NAME_ERROR ) ; return false ; } queryEntry ( username , function ( host , port ) { netNode . connect ( { host : host , port : port , handShakeReq : { } } , function ( ) { var route = "connector.entryHandler.enter" ; netNode . request ( route , { username : username , rid : rid } , function ( dpkg ) { if ( dpkg . errorMsg ) { showError ( DUPLICATE_ERROR ) ; return ; } setName ( ) ; setRoom ( ) ; showChat ( ) ; initUserList ( dpkg ) ; } ) ; } ) ; } ) ; } ) ; $ ( "#errbtntest" ) . click ( ( ) => { var route = "chat.chatHandler.send" ; netNode . request ( route , { content : $ ( "#entry" ) . attr ( "value" ) . replace ( "\n" , "" ) , rid : rid , } , dpkg => { console . warn ( "!!?? " , dpkg ) ; } ) ; } ) ; $ ( "#entry" ) . keypress ( function ( e ) { var route = "chat.chatHandler.send" ; var target = $ ( "#usersList" ) . val ( ) ; if ( e . keyCode != 13 ) return ; var msg = $ ( "#entry" ) . attr ( "value" ) . replace ( "\n" , "" ) ; if ( ! util . isBlank ( msg ) ) { netNode . request ( route , { rid : rid , content : msg , from : username , target : target } , function ( dpkg ) { $ ( "#entry" ) . attr ( "value" , "" ) ; if ( target != '*' && target != username ) { addMessage ( username , target , msg ) ; $ ( "#chatHistory" ) . show ( ) ; } } ) ; } } ) ; } ) ;