diff --git a/src/SuperSocket.WebSocket/WebSocketEncoder.cs b/src/SuperSocket.WebSocket/WebSocketEncoder.cs index feb821634..01ea9c678 100644 --- a/src/SuperSocket.WebSocket/WebSocketEncoder.cs +++ b/src/SuperSocket.WebSocket/WebSocketEncoder.cs @@ -238,11 +238,11 @@ private int EncodeFinalFragment(IBufferWriter writer, byte opCode, ReadOnl { var totalWritten = 0; + // writer should not be touched for now, because head has not been written yet. + encodingContext = CreateDataEncodingContext(null); + if (encoder != null) { - // writer should not be touched for now, because head has not been written yet. - encodingContext = CreateDataEncodingContext(null); - var fragementSize = (text.Length > 0 ? encoder.GetByteCount(text, true) : 0) + unwrittenBytes.Count; if (fragementSize == 0) diff --git a/src/SuperSocket.WebSocket/WebSocketMaskedEncoder.cs b/src/SuperSocket.WebSocket/WebSocketMaskedEncoder.cs index d3bd6c669..02cc42f7c 100644 --- a/src/SuperSocket.WebSocket/WebSocketMaskedEncoder.cs +++ b/src/SuperSocket.WebSocket/WebSocketMaskedEncoder.cs @@ -41,12 +41,8 @@ protected override object CreateDataEncodingContext(IBufferWriter writer) protected override Span WriteHead(IBufferWriter writer, long length, out int headLen) { - var head = base.WriteHead(writer, length, out headLen); - - // We don't mask data for empty package - if (length > 0) - head[1] = (byte)(head[1] | 0x80); - + var head = base.WriteHead(writer, length, out headLen); + head[1] = (byte)(head[1] | 0x80); return head; }