Skip to content

Instantly share code, notes, and snippets.

@azenla
Last active August 29, 2015 14:17
Show Gist options
  • Save azenla/51c33cc790ff5fca8336 to your computer and use it in GitHub Desktop.
Save azenla/51c33cc790ff5fca8336 to your computer and use it in GitHub Desktop.
import "dart:async";
import "dart:io";
import "dart:math";
String generateToken(int length) {
var r = new Random();
var buffer = new StringBuffer();
for (int i = 1; i <= length; i++) {
if (r.nextBool()) {
String letter = alphabet[r.nextInt(alphabet.length)];
buffer.write(r.nextBool() ? letter.toLowerCase() : letter);
} else {
buffer.write(numbers[r.nextInt(numbers.length)]);
}
}
return buffer.toString();
}
main() async {
var server = await ServerSocket.bind(InternetAddress.ANY_IP_V4, 1234);
server.listen(handleClient);
}
handleClient(Socket client) async {
new Timer.periodic(const Duration(seconds: 2), (timer) {
client.write(generateToken(50));
});
}
const List<String> alphabet = const [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z"
];
const List<int> numbers = const [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment