1+ let modules = {
2+ "WebSocket" : "ws"
3+ }
4+ for ( let module in modules ) {
5+ let module_name = module ;
6+ let module_path = modules [ module_name ] ;
7+ try {
8+ require ( module_path ) ;
9+ } catch ( e ) {
10+ throw new Error ( `导入${ module_name } 失败(位于${ module_path } )` )
11+ }
12+ global [ module_name ] = require ( module_path ) ;
13+ }
14+
15+ class Client {
16+ constructor ( server_ip ) {
17+ this . _ws = new WebSocket ( server_ip ) ;
18+ this . _ws . onopen = ( ) => {
19+ this . onjoin ( ) ;
20+ }
21+ this . _ws . onclose = ( ) => {
22+ this . joined = false ;
23+ this . onclose ( this . _closereason ) ;
24+ delete this . _closereason ;
25+ }
26+ this . _ws . onmessage = ( event ) => {
27+ let hc = JSON . parse ( event . data ) ;
28+ if ( hc . cmd == "onlineSet" ) {
29+ let tnick = [ ...hc . nicks ] ;
30+ this . nick = tnick . pop ( ) ;
31+ if ( ! this . joined ) {
32+ setInterval ( ( ) => {
33+ if ( this . ping <= 0 ) return ;
34+ let lastpingtime = this . pingtime || 0 ;
35+ if ( lastpingtime + this . ping < ( new Date ( ) ) . getTime ( ) ) {
36+ this . pingtime = ( new Date ( ) ) . getTime ( ) ;
37+ this . check = true ;
38+ this . _send ( {
39+ cmd : '' //rl最小
40+ } )
41+ }
42+
43+ } , 1000 )
44+ }
45+ this . joined = true ;
46+ this . onjoined ( )
47+ }
48+ if ( hc . cmd == "warn" ) {
49+ setTimeout ( ( ) => {
50+ if ( ! this . joined ) this . joinfailed ( hc . text )
51+ } , 500 ) ;
52+ }
53+ if ( hc . channel ) {
54+ if ( hc . channel != this . channel ) {
55+ this . channel = hc . channel ;
56+ this . onchangechannel ( hc . channel ) ;
57+ }
58+ }
59+ if ( hc . cmd == "warn" && hc . text . startsWith ( `Command not found, did you mean: ` ) && this . check ) {
60+ this . check = false ;
61+ return ;
62+ }
63+ if ( hc . cmd == "onlineSet" ) {
64+ this . users = hc . users
65+ this . nicks = hc . nicks
66+ }
67+ if ( hc . cmd == "onlineAdd" ) {
68+ let payload = { ...hc }
69+ delete payload . cmd
70+ this . users . push ( payload )
71+ this . nicks . push ( hc . nick )
72+ this . nicks = [ ...new Set ( this . nicks ) ]
73+ }
74+ if ( hc . cmd == "onlineRemove" ) {
75+ this . users = this . users . filter ( function ( item ) {
76+ return item . nick !== hc . nick ;
77+ } ) ;
78+ let index = this . nicks . indexOf ( hc . nick ) ;
79+ if ( index !== - 1 ) this . nicks . splice ( index , 1 ) ;
80+ this . nicks = [ ...new Set ( this . nicks ) ]
81+ }
82+ if ( hc . cmd == "updateUser" ) {
83+ let payload = { ...hc }
84+ delete payload . cmd
85+ this . users = this . users . filter ( function ( item ) {
86+ return item . nick !== hc . nick ;
87+ } ) ;
88+ this . users . push ( payload )
89+ }
90+ if ( hc . cmd == "info" && / ^ ( [ a - z A - Z 0 - 9 _ ] { 1 , 24 } ) i s n o w ( [ a - z A - Z 0 - 9 _ ] { 1 , 24 } ) $ / . test ( hc . text ) ) {
91+ let changenickuser = hc . text . match ( / ^ ( [ a - z A - Z 0 - 9 _ ] { 1 , 24 } ) i s n o w ( [ a - z A - Z 0 - 9 _ ] { 1 , 24 } ) $ / ) ;
92+ if ( changenickuser [ 1 ] == this . nick ) {
93+ this . nick = changenickuser [ 2 ] ;
94+ }
95+ return this . onmessage (
96+ event . data ,
97+ {
98+ cmd : 'changeNick' ,
99+ nick : changenickuser [ 1 ] ,
100+ to_nick : changenickuser [ 2 ]
101+ }
102+ )
103+ }
104+ if ( hc . cmd == "emote" ) hc . msg = hc . text . substring ( hc . nick . length + 2 ) ;
105+ if ( hc . cmd == "info" && hc . type == "whisper" && typeof hc . from == "string" ) {
106+ hc . nick = hc . from ;
107+ hc . msg = hc . text . substring ( hc . from . length + 12 ) ;
108+ }
109+ this . onmessage ( event . data , hc ) ;
110+ }
111+ [ "onclose" , "onchangechannel" , "joinfailed" , "oncaptcha" , "onjoin" , "onjoined" , "onmessage" ] . forEach ( func => {
112+ this [ func ] = ( ) => { }
113+ } ) ;
114+ [ "channel" , "nick" , "joined" , "ping" , "pingtime" , "check" ] . forEach ( ve => {
115+ this [ ve ] = false
116+ } ) ;
117+ this . customId = 0 ;
118+ }
119+ join ( pack ) {
120+ return new Promise ( resolve => {
121+ if ( this . _ws . readyState !== 1 ) resolve ( false ) ; //连接没有打开,加入失败
122+ pack . cmd = "join" ;
123+ this . channel = pack . channel ;
124+ this . _send ( pack ) ;
125+ } )
126+ }
127+ _send ( obj ) {
128+ this . _ws . send ( JSON . stringify ( obj ) ) ;
129+ }
130+ close ( reason ) {
131+ this . _closereason = reason ;
132+ this . _ws . close ( ) ;
133+ }
134+ chat ( text , custom_id = false ) {
135+ let pack = {
136+ cmd : 'chat' ,
137+ text : text
138+ }
139+ if ( custom_id ) pack . customId = custom_id ;
140+ this . _send ( pack ) ;
141+ return true ;
142+ }
143+ updatemessage ( custom_id , mode , text ) {
144+ if ( ! custom_id . toString ( ) || ! mode ) return false ;
145+ if ( ! [ "overwrite" , "append" , "prepend" , "complete" ] . includes ( mode ) ) return false ;
146+ this . _send ( {
147+ cmd : 'updateMessage' ,
148+ customId : custom_id . toString ( ) ,
149+ mode : mode ,
150+ text : text
151+ } )
152+ }
153+ getcustomId ( ) {
154+ return customId . toString ( ) ;
155+ customId += 1 ;
156+ }
157+ }
158+
159+ module . exports = Client
0 commit comments