@@ -116,11 +116,13 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
116116 } ) ;
117117 }
118118
119- this . client . onopen = ( ) => {
119+ this . client . onopen = async ( ) => {
120120 this . clientConnected ( ) ;
121121
122122 this . setupPingPong ( ) ;
123123
124+ await this . getMaxPayloadSize ( ) ;
125+
124126 return resolve ( ) ;
125127 } ;
126128
@@ -241,9 +243,15 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
241243 * @param {Object } payload
242244 */
243245 send ( request : RequestPayload , options : JSONObject = { } ) {
246+ if ( this . maxPayloadSize !== null && Buffer . byteLength ( JSON . stringify ( request ) , 'utf8' ) > this . maxPayloadSize ) {
247+
248+ const error : any = new Error (
249+ `Payload size exceeded the maximum allowed by the server ${ this . maxPayloadSize } bytes`
250+ ) ;
251+
252+ this . emit ( "networkError" , { error } ) ;
253+ this . clientDisconnected ( DisconnectionOrigin . PAYLOAD_MAX_SIZE_EXCEEDED ) ;
244254
245- if ( this . maxPayloadSize && Buffer . byteLength ( JSON . stringify ( request ) , 'utf8' ) > this . maxPayloadSize ) {
246- this . clientNetworkError ( new Error ( 'Payload size exceeds the maximum allowed size of ' + this . maxPayloadSize ) ) ;
247255 return ;
248256 }
249257
@@ -351,7 +359,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
351359 // If we were waiting for a pong that never occured before the next ping cycle we throw an error
352360 if ( this . waitForPong ) {
353361 const error : any = new Error (
354- "Kuzzle does'nt respond to ping. Connection lost."
362+ "Kuzzle doesn't respond to ping. Connection lost."
355363 ) ;
356364 error . status = 503 ;
357365
@@ -368,10 +376,13 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
368376 }
369377 } , this . _pingInterval ) ;
370378 }
379+ /**
380+ * Get the maximum payload size allowed by the server
381+ * Stores the value in `this.maxPayloadSize`
382+ **/
371383 async getMaxPayloadSize ( ) {
372384 return new Promise ( ( resolve , reject ) => {
373385 const originalOnMessage = this . client . onmessage ;
374-
375386 this . client . onmessage = ( payload ) => {
376387 try {
377388 const data = JSON . parse ( payload . data || payload ) ;
0 commit comments