@@ -215,8 +215,14 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
215215 await ReadNextFrameAsync ( cancellationToken ) ;
216216 }
217217
218- // Handle fragmentation, remember the first frame type
219218 int opCode = _frameInProgress . OpCode ;
219+
220+ if ( opCode == Constants . OpCodes . CloseFrame )
221+ {
222+ return await ProcessCloseFrameAsync ( cancellationToken ) ;
223+ }
224+
225+ // Handle fragmentation, remember the first frame type
220226 if ( opCode == Constants . OpCodes . ContinuationFrame )
221227 {
222228 if ( ! _firstDataOpCode . HasValue )
@@ -230,11 +236,6 @@ public async override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byt
230236 _firstDataOpCode = opCode ;
231237 }
232238
233- if ( opCode == Constants . OpCodes . CloseFrame )
234- {
235- return await ProcessCloseFrameAsync ( cancellationToken ) ;
236- }
237-
238239 // Make sure there's at least some data in the buffer
239240 int bytesToBuffer = ( int ) Math . Min ( ( long ) _receiveBuffer . Length , _frameBytesRemaining ) ;
240241 await EnsureDataAvailableOrReadAsync ( bytesToBuffer , cancellationToken ) ;
@@ -329,6 +330,11 @@ private async Task ReadNextFrameAsync(CancellationToken cancellationToken)
329330 _frameInProgress = null ;
330331 }
331332 }
333+ else if ( _firstDataOpCode . HasValue && _frameInProgress . OpCode != Constants . OpCodes . ContinuationFrame )
334+ {
335+ // A data frame is already in progress, but this new frame is not a continuation frame.
336+ await SendErrorAbortAndThrow ( WebSocketCloseStatus . ProtocolError , "Expected a continuation frame: " + _frameInProgress . OpCode , cancellationToken ) ;
337+ }
332338 }
333339
334340 private async Task EnsureDataAvailableOrReadAsync ( int bytesNeeded , CancellationToken cancellationToken )
0 commit comments