1- import http from 'http'
2- import Ws from 'ws'
1+ import http from 'http' ;
2+ import Ws from 'ws' ;
33import { config , jwt } from 'lin-mizar' ;
4- import { URLSearchParams } from 'url'
5- import { set , get } from 'lodash'
4+ import { URLSearchParams } from 'url' ;
5+ import { set , get } from 'lodash' ;
66import { UserGroupModel } from '../../model/user-group' ;
77
8- const USER_KEY = Symbol ( 'user' )
8+ const USER_KEY = Symbol ( 'user' ) ;
99
10- const INTERCEPTORS = Symbol ( 'WebSocket#interceptors' )
10+ const INTERCEPTORS = Symbol ( 'WebSocket#interceptors' ) ;
1111
12- const HANDLE_CLOSE = Symbol ( 'WebSocket#close' )
12+ const HANDLE_CLOSE = Symbol ( 'WebSocket#close' ) ;
1313
14- const HANDLE_ERROR = Symbol ( 'WebSocket#error' )
14+ const HANDLE_ERROR = Symbol ( 'WebSocket#error' ) ;
1515
1616class WebSocket {
17- constructor ( app ) {
18- this . app = app
19- this . wss = null
20- this . sessions = new Set ( )
17+ constructor ( app ) {
18+ this . app = app ;
19+ this . wss = null ;
20+ this . sessions = new Set ( ) ;
2121 }
2222
2323 /**
2424 * 初始化,挂载 socket
2525 */
26- init ( ) {
27- const server = http . createServer ( this . app . callback ( ) )
26+ init ( ) {
27+ const server = http . createServer ( this . app . callback ( ) ) ;
2828 this . wss = new Ws . Server ( {
2929 path : config . getItem ( 'socket.path' , '/ws/message' ) ,
3030 noServer : true
31- } )
31+ } ) ;
3232
3333 server . on ( 'upgrade' , this [ INTERCEPTORS ] . bind ( this ) ) ;
3434
35- this . wss . on ( 'connection' , ( socket ) => {
36- socket . on ( 'close' , this [ HANDLE_CLOSE ] . bind ( this ) )
37- socket . on ( 'error' , this [ HANDLE_ERROR ] . bind ( this ) )
38- } )
35+ this . wss . on ( 'connection' , socket => {
36+ socket . on ( 'close' , this [ HANDLE_CLOSE ] . bind ( this ) ) ;
37+ socket . on ( 'error' , this [ HANDLE_ERROR ] . bind ( this ) ) ;
38+ } ) ;
3939
40- this . app . context . websocket = this
41- return server
40+ this . app . context . websocket = this ;
41+ return server ;
4242 }
4343
44- [ INTERCEPTORS ] ( request , socket , head ) {
44+ [ INTERCEPTORS ] ( request , socket , head ) {
4545 // 是否开启 websocket 的鉴权拦截器
4646 if ( config . getItem ( 'socket.intercept' ) ) {
47- const params = new URLSearchParams ( request . url . slice ( request . url . indexOf ( '?' ) ) )
48- const token = params . get ( 'token' )
47+ const params = new URLSearchParams (
48+ request . url . slice ( request . url . indexOf ( '?' ) )
49+ ) ;
50+ const token = params . get ( 'token' ) ;
4951 try {
50- const { identity } = jwt . verifyToken ( token )
51- this . wss . handleUpgrade ( request , socket , head , ( ws ) => {
52- set ( ws , USER_KEY , identity )
53- this . sessions . add ( ws )
52+ const { identity } = jwt . verifyToken ( token ) ;
53+ this . wss . handleUpgrade ( request , socket , head , ws => {
54+ set ( ws , USER_KEY , identity ) ;
55+ this . sessions . add ( ws ) ;
5456 this . wss . emit ( 'connection' , ws , request ) ;
55- } )
57+ } ) ;
5658 } catch ( error ) {
57- console . log ( error . message )
58- socket . destroy ( )
59+ console . log ( error . message ) ;
60+ socket . destroy ( ) ;
5961 }
60- return
62+ return ;
6163 }
62- this . wss . handleUpgrade ( request , socket , head , ( ws ) => {
63- this . sessions . add ( ws )
64+ this . wss . handleUpgrade ( request , socket , head , ws => {
65+ this . sessions . add ( ws ) ;
6466 this . wss . emit ( 'connection' , ws , request ) ;
65- } )
67+ } ) ;
6668 }
6769
68- [ HANDLE_CLOSE ] ( ) {
70+ [ HANDLE_CLOSE ] ( ) {
6971 for ( const session of this . sessions ) {
7072 if ( session . readyState === Ws . CLOSED ) {
71- this . sessions . delete ( session )
73+ this . sessions . delete ( session ) ;
7274 }
7375 }
7476 }
7577
76- [ HANDLE_ERROR ] ( session , error ) {
77- console . log ( error )
78+ [ HANDLE_ERROR ] ( session , error ) {
79+ console . log ( error ) ;
7880 }
7981
8082 /**
@@ -83,14 +85,14 @@ class WebSocket {
8385 * @param {number } userId 用户id
8486 * @param {string } message 消息
8587 */
86- sendMessage ( userId , message ) {
88+ sendMessage ( userId , message ) {
8789 for ( const session of this . sessions ) {
8890 if ( session . readyState === Ws . OPEN ) {
89- continue
91+ continue ;
9092 }
9193 if ( get ( session , USER_KEY ) === userId ) {
92- session . send ( message )
93- break
94+ session . send ( message ) ;
95+ break ;
9496 }
9597 }
9698 }
@@ -100,64 +102,64 @@ class WebSocket {
100102 *
101103 * @param {WebSocket } session 当前会话
102104 * @param {string } message 消息
103- */
104- sendMessageToSession ( session , message ) {
105- session . send ( message )
105+ */
106+ sendMessageToSession ( session , message ) {
107+ session . send ( message ) ;
106108 }
107109
108110 /**
109111 * 广播
110- *
111- * @param {string } message 消息
112+ *
113+ * @param {string } message 消息
112114 */
113- broadCast ( message ) {
115+ broadCast ( message ) {
114116 this . sessions . forEach ( session => {
115117 if ( session . readyState === Ws . OPEN ) {
116- session . send ( message )
118+ session . send ( message ) ;
117119 }
118- } )
120+ } ) ;
119121 }
120122
121123 /**
122124 * 对某个分组广播
123- *
125+ *
124126 * @param {number } 分组id
125127 * @param {string } 消息
126128 */
127- async broadCastToGroup ( groupId , message ) {
129+ async broadCastToGroup ( groupId , message ) {
128130 const userGroup = await UserGroupModel . findAll ( {
129131 where : {
130132 group_id : groupId
131133 }
132- } )
133- const userIds = userGroup . map ( v => v . getDataValue ( 'user_id' ) )
134+ } ) ;
135+ const userIds = userGroup . map ( v => v . getDataValue ( 'user_id' ) ) ;
134136 for ( const session of this . sessions ) {
135137 if ( session . readyState !== Ws . OPEN ) {
136- continue
138+ continue ;
137139 }
138- const userId = get ( session , USER_KEY )
140+ const userId = get ( session , USER_KEY ) ;
139141 if ( ! userId ) {
140- continue
142+ continue ;
141143 }
142144 if ( userIds . includes ( userId ) ) {
143- session . send ( message )
145+ session . send ( message ) ;
144146 }
145147 }
146148 }
147149
148150 /**
149151 * 获取所有会话
150152 */
151- getSessions ( ) {
152- return this . sessions
153+ getSessions ( ) {
154+ return this . sessions ;
153155 }
154156
155157 /**
156158 * 获得当前连接数
157159 */
158- getConnectionCount ( ) {
159- return this . sessions . size
160+ getConnectionCount ( ) {
161+ return this . sessions . size ;
160162 }
161163}
162164
163- export default WebSocket
165+ export default WebSocket ;
0 commit comments