handshake method

void handshake(
  1. Uint8List data,
  2. WebSocketChannel channel
)

Implementation

void handshake(Uint8List data, WebSocketChannel channel) {
  var secretKey = String.fromCharCodes(data.toList())
      .split('\r\n')
      .firstWhere((element) => element.contains('Sec-WebSocket-Key: '))
      .split(': ')
      .last;
  var acceptKey = convert.base64.encode(sha1
      .convert((secretKey + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').codeUnits)
      .bytes);
  List<String> head = [];
  head.add('HTTP/1.1 101 Switching Protocols\r\n');
  head.add('Upgrade: websocket\r\n');
  head.add('Connection: Upgrade\r\n');
  head.add('Sec-WebSocket-Accept: $acceptKey\r\n');
  head.add('\r\n');

  channel.socket.write(head.join());
  channel.handshaked = true;
  provider?.onConnected(channel);
}