Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use v6;
- use Data::Dump;
- use experimental :pack;
- use JSON::Tiny;
- use Redis::Async;
- constant $SOCKET_PORT = 7000;
- constant $SOCKET_ADDR = '0.0.0.0';
- constant $REDIS_PORT = 6379;
- constant $REDIS_ADDR = '127.0.0.1';
- constant $REDIS_AUTH = 'xxxxxxxx';
- constant $IDLING_PERIOD_MIN = 180 - 2; # 3 minutes - 2 secs
- constant $CACHE_EXPIRE_IN = 86400; # 24h hours
- # create socket
- my $socket = IO::Socket::Async.listen($SOCKET_ADDR, $SOCKET_PORT);
- # connnect to Redis ...
- my $redis;
- try {
- my $error-code = "110";
- $redis = Redis::Async.new("$SOCKET_ADDR:$SOCKET_PORT");
- $redis.auth($REDIS_AUTH);
- CATCH {
- default {
- say "Error $error-code ", .^name, ': Failed to initiate connection to Redis';
- exit;
- }
- }
- }
- # react whenever there is connection
- react {
- whenever $socket -> $conn {
- # do something when the connection wants to talk
- whenever $conn.Supply(:bin) {
- # only process if data length is either 108 or 116
- if $_.decode('utf-8').chars == 108 or $_.decode('utf-8').chars == 116 {
- say "Received --> "~$_.decode('utf-8');
- my $ack = generateAck($_.decode('utf-8')); # generate ack based on received data
- if $ack {
- $conn.print: $ack;
- }else{
- say "No ack. Received data maybe corrupted. Closing connection";
- $conn.close;
- }
- }
- }
- }
- CATCH {
- default {
- say .^name, ': ', .Str;
- say "handled in $?LINE";
- }
- }
- }
- ### other subroutines down here ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement