@@ -10,6 +10,7 @@ const getOrCreateNamespace = (namespaces, name) => {
1010 name,
1111 sockets : [ ] ,
1212 rooms : [ ] ,
13+ events : [ ] ,
1314 } ;
1415 namespaces . push ( namespace ) ;
1516 return namespace ;
@@ -64,6 +65,17 @@ const addSocket = (state, socket) => {
6465 }
6566} ;
6667
68+ const MAX_ARRAY_LENGTH = 1000 ;
69+ let EVENT_COUNTER = 0 ;
70+
71+ const pushEvents = ( array , event ) => {
72+ event . eventId = ++ EVENT_COUNTER ; // unique id
73+ array . push ( event ) ;
74+ if ( array . length > MAX_ARRAY_LENGTH ) {
75+ array . shift ( ) ;
76+ }
77+ } ;
78+
6779export default {
6880 namespaced : true ,
6981 state : {
@@ -97,6 +109,9 @@ export default {
97109 rooms : ( state ) => {
98110 return state . selectedNamespace ? state . selectedNamespace . rooms : [ ] ;
99111 } ,
112+ events : ( state ) => {
113+ return state . selectedNamespace ? state . selectedNamespace . events : [ ] ;
114+ } ,
100115 } ,
101116 mutations : {
102117 selectNamespace ( state , namespace ) {
@@ -114,8 +129,14 @@ export default {
114129 find ( state . namespaces , { name : "/" } ) || state . namespaces [ 0 ] ;
115130 }
116131 } ,
117- onSocketConnected ( state , socket ) {
132+ onSocketConnected ( state , { timestamp , socket } ) {
118133 addSocket ( state , socket ) ;
134+ const namespace = getOrCreateNamespace ( state . namespaces , socket . nsp ) ;
135+ pushEvents ( namespace . events , {
136+ type : "connection" ,
137+ timestamp,
138+ id : socket . id ,
139+ } ) ;
119140 } ,
120141 onSocketUpdated ( state , socket ) {
121142 const namespace = getOrCreateNamespace ( state . namespaces , socket . nsp ) ;
@@ -124,7 +145,7 @@ export default {
124145 merge ( existingSocket , socket ) ;
125146 }
126147 } ,
127- onSocketDisconnected ( state , { nsp, id } ) {
148+ onSocketDisconnected ( state , { timestamp , nsp, id, reason } ) {
128149 const namespace = getOrCreateNamespace ( state . namespaces , nsp ) ;
129150 const [ socket ] = remove ( namespace . sockets , { id } ) ;
130151 if ( socket ) {
@@ -137,17 +158,29 @@ export default {
137158 remove ( state . clients , { id : socket . clientId } ) ;
138159 }
139160 }
161+ pushEvents ( namespace . events , {
162+ type : "disconnection" ,
163+ timestamp,
164+ id,
165+ args : reason ,
166+ } ) ;
140167 } ,
141- onRoomJoined ( state , { nsp, room, id } ) {
168+ onRoomJoined ( state , { nsp, room, id, timestamp } ) {
142169 const namespace = getOrCreateNamespace ( state . namespaces , nsp ) ;
143170 const socket = find ( namespace . sockets , { id } ) ;
144171 if ( socket ) {
145172 pushUniq ( socket . rooms , room ) ;
146173 const _room = getOrCreateRoom ( namespace , room ) ;
147174 _room . sockets . push ( socket ) ;
148175 }
176+ pushEvents ( namespace . events , {
177+ type : "room_joined" ,
178+ timestamp,
179+ id,
180+ args : room ,
181+ } ) ;
149182 } ,
150- onRoomLeft ( state , { nsp, room, id } ) {
183+ onRoomLeft ( state , { timestamp , nsp, room, id } ) {
151184 const namespace = getOrCreateNamespace ( state . namespaces , nsp ) ;
152185 const socket = find ( namespace . sockets , { id } ) ;
153186 if ( socket ) {
@@ -159,6 +192,12 @@ export default {
159192 _room . active = false ;
160193 remove ( namespace . rooms , { name : room } ) ;
161194 }
195+ pushEvents ( namespace . events , {
196+ type : "room_left" ,
197+ timestamp,
198+ id,
199+ args : room ,
200+ } ) ;
162201 } ,
163202 } ,
164203} ;
0 commit comments