private void ParseFrame(NetworkDataEventArgs e) { var data = new List <byte>(); var payloadStartIndex = 2 + _parser.PayloadLenLenght + _parser.MaskingKeyLenght; switch ((WebSocketOpcode)_parser.Opcode) { case WebSocketOpcode.Ping: //Ping frame. Sent Pong Frame. _parser.PollReceived = true; data.AddRange(_parser.DataBuffer); data[0] = (byte)(data[0] & 0xF0); data[0] = (byte)(data[0] | (byte)WebSocketOpcode.Pong); Write(data.ToArray()); break; case WebSocketOpcode.Pong: _parser.PollReceived = true; break; case WebSocketOpcode.ConnectionClose: //Close frame. Sent Close back. var connectionCloseReason = (_parser.DataBuffer[payloadStartIndex] << 8) + ((int)_parser.DataBuffer[payloadStartIndex + 1] << 0); ConnectionCloseEvent?.Invoke(this, new ConnectionCloseEventArgs((ConnectionCloseReason)connectionCloseReason)); SendClose((ConnectionCloseReason)connectionCloseReason); break; case WebSocketOpcode.BinaryFrame: case WebSocketOpcode.TextFrame: DataReceivedEvent?.Invoke(this, new DataReceivedEventArgs(e.LocalEndPoint, e.RemoteEndPoint, (WebSocketOpcode)_parser.Opcode, _parser.DataBuffer.GetRange(payloadStartIndex, _parser.PayloadLen).ToArray())); break; } }
public void Close() { ConnectionCloseEvent?.Invoke(this); receiving = false; connected = false; client.Close(); }