Moved to using websockets
This commit is contained in:
parent
d7c708aab3
commit
3cf8fa031f
@ -1,79 +0,0 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use JSON;
|
||||
# use database engine
|
||||
|
||||
sub get_request_info {
|
||||
my $clientIP = ($ENV{"REMOTE_ADDR"} or "0.0.0.0");
|
||||
my $proto = ($ENV{"REQUEST_SCHEME"} or "http");
|
||||
my $get_query = (split(/\?/,($ENV{"QUERY_STRING"} or "")))[0];
|
||||
my $post_query = "";
|
||||
if ( $ENV{"CONTENT_LENGTH"} ) { read( <STDIN>, $post_query, $ENV{"CONTENT_LENGTH"}); }
|
||||
my $referrer = ($ENV{"HTTP_REFERER"} or "");
|
||||
return ($proto,$clientIP,$get_query,$post_query,$referrer);
|
||||
}
|
||||
|
||||
sub http_status {
|
||||
my ($status,$content,$target) = @_;
|
||||
my $header = "";
|
||||
$header .= "status: ".$status."\r\n";
|
||||
$header .= "Location: ".$target."\r\n" if $target;
|
||||
$header .= "Content-Type: ".$content."\r\n" if $content;
|
||||
$header .= "\r\n";
|
||||
return $header;
|
||||
}
|
||||
|
||||
sub html_tag {
|
||||
if ($_[1]) { return "<".($_[0] or "div").($_[2] or "").">".($_[1] or "")."</".($_[0] or "div").">\n";
|
||||
} else {return "<".($_[0] or "div").($_[2] or "")." />\n";
|
||||
}
|
||||
}
|
||||
|
||||
sub html_content {
|
||||
return html_tag("html", html_tag( "head", html_tag( "title", shift ) . shift ) . html_tag( "body", shift ) );
|
||||
}
|
||||
|
||||
sub soft_die {
|
||||
print http_status(500,"text/html; charset=utf-8");
|
||||
print html_content("500","",shift);
|
||||
exit;
|
||||
}
|
||||
|
||||
sub clean_input {
|
||||
my $input = shift;
|
||||
if ($input =~ m!%2F!) { soft_die( "Location/hax\r\n\r\n"; }
|
||||
$input =~ s!%(..)!chr hex $1!ge;
|
||||
$input =~ s!\+! !g;
|
||||
return $input;
|
||||
}
|
||||
|
||||
my @request = get_request_info();
|
||||
my @get_params = split( "!", clean_input($request[2]) );
|
||||
# my @post_params
|
||||
my $directive = shift(@get_params);
|
||||
if ( "new" eq $directive ) {
|
||||
# new session, new deck, new location
|
||||
# if deck/location parent sessionID
|
||||
# if location public/private
|
||||
# returns id and key of new object
|
||||
} elsif ( "shuffle" eq $directive ) {
|
||||
# shuffle - deckID - full/current - deckKey
|
||||
# sheffles all cards in deckID's locationID if deckKey matches
|
||||
} elsif ( "move" eq $directive ) {
|
||||
# move - cardID - current locationID - current locationKey - destination locationID
|
||||
# changes ownership of cardID to destination locationID if cardID is owned by current locationID and the current locationKey matchs
|
||||
} elsif ( "getLocations" eq $directive ) {
|
||||
# getLocations - sessionID - sessionKey
|
||||
# return all locationIDs associated with sessionID as long as sessionKey matches
|
||||
} elsif ( "getCards" eq $directive ) {
|
||||
# getCards - locationID - locationKey
|
||||
# returns all cardIDs for locationID as long as its public or the locationKey matches
|
||||
} elsif ( "getDecks" eq $directive ) {
|
||||
# getDecks - sessionID - sessionKey
|
||||
# returns all deckIDs associated with sessionID provided sessionKey matchs.
|
||||
} else {
|
||||
print http_status(200,"text/json");
|
||||
print '{"error":"No input or incorrect input", "input":"'.$directive.'!'.join("!",@get_params).'"}';
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
create tablespace deckard_space
|
||||
OWNER deckard
|
||||
LOCATION '\tmp\deckard';
|
||||
create database deckard WITH
|
||||
OWNER=deckard
|
||||
TABLESPACE=deckard_space;
|
||||
create table sessions (
|
||||
sessionID UUID PRIMARY KEY,
|
||||
sessionKey VARCHAR (256) NOT NULL,
|
||||
last_update TIMESTAMP NOT NULL
|
||||
);
|
||||
create table locations (
|
||||
locationID UUID PRIMARY KEY,
|
||||
locationKey VARCHAR (256) NOT NULL,
|
||||
sessionID UUID REFERENCES sessions(sessionID),
|
||||
locationType text CHECK (locationType = 'deck' or 'hand' or 'discard'),
|
||||
showPublic boolean NOT NULL
|
||||
);
|
||||
create table decks (
|
||||
deckID UUID PRIMPARY KEY,
|
||||
deckKey VARCHAR (256) NOT NULL,
|
||||
sessionID UUID REFERENCES sessions(sessionID),
|
||||
locationID UUID REFERENCES locations(locationID)
|
||||
);
|
||||
create table cards (
|
||||
cardID UUID PRIMARY KEY
|
||||
sessionID UUID REFERENCES sessions(sessionID),
|
||||
locationID UUID REFERENCES locations(locationID),
|
||||
deckID UUID REFERENCES decks(deckID),
|
||||
cardContent text NOT NULL,
|
||||
position integer NOT NULL
|
||||
);
|
@ -3,8 +3,7 @@ This project is an exansion on my original idea with [Deckard](https://labs.murk
|
||||
|
||||
### Goals:
|
||||
|
||||
* Implement database backend
|
||||
* Implement API backend
|
||||
* Implement websocket server backend
|
||||
* Session creation
|
||||
* Deck upload and sanitization
|
||||
* Multipul Mixed and Separate decks per session
|
||||
@ -21,4 +20,4 @@ Not yet written
|
||||
|
||||
### Original Deckard project
|
||||
|
||||
[For developing new decks](https://labs.murkfall.net/bluesaxman/deckard)
|
||||
[For developing new decks](https://labs.murkfall.net/bluesaxman/deckard)
|
||||
|
71
Server/server.pl
Executable file
71
Server/server.pl
Executable file
@ -0,0 +1,71 @@
|
||||
#!/usr/bin/perl -w
|
||||
use strict;
|
||||
use warnings;
|
||||
use Net::WebSocket::Server;
|
||||
use JSON;
|
||||
|
||||
my $origin = 'ws://localhost';
|
||||
my %sessions = ();
|
||||
|
||||
sub generateSession {
|
||||
my $newSession = shift;
|
||||
unless (defined $newSession) { $newSession = time; }
|
||||
if ( grep( /$newSession/, keys %sessions ) ) {
|
||||
generateSession();
|
||||
} else {
|
||||
print "Adding session ".$newSession." to database\n";
|
||||
$sessions{$newSession} = ();
|
||||
$sessions{$newSession}{"id"} = $newSession;
|
||||
$sessions{$newSession}{"users"} = [];
|
||||
$sessions{$newSession}{"decks"} = ();
|
||||
$sessions{$newSession}{"pools"} = ();
|
||||
|
||||
}
|
||||
return $newSession;
|
||||
}
|
||||
|
||||
sub joinSession {
|
||||
my ($sessionID, $conn) = @_;
|
||||
print "Connection attempts to use session ".$sessionID."\n";
|
||||
unless ( grep( /$sessionID/, keys %sessions ) ) {
|
||||
print "Session ".$sessionID." does not yet exsist, creating...\n";
|
||||
$sessionID = generateSession($sessionID);
|
||||
}
|
||||
push(@{$sessions{$sessionID}{"users"}}, $conn->{"initHand"}->res->key);
|
||||
$conn->{"currentSession"} = $sessions{$sessionID};
|
||||
print "A client has connected to session: ".$conn->{"currentSession"}{"id"}."\n";
|
||||
}
|
||||
|
||||
Net::WebSocket::Server->new(
|
||||
listen => 8080,
|
||||
on_connect => sub {
|
||||
my ($serv, $conn) = @_;
|
||||
$conn->on(
|
||||
handshake => sub {
|
||||
my ($conn,$handshake) = @_;
|
||||
$conn->{"initHand"} = $handshake;
|
||||
},
|
||||
ready => sub {
|
||||
my ($conn) = @_;
|
||||
#strip the / off of the resource request and then assign just the request to $session
|
||||
my $session = (split("/", $conn->{"initHand"}->req->resource_name))[1];
|
||||
unless ($session) { $conn->send_utf8(generateSession()); $conn->disconnect(); print "Severed client, no session given\n"; }
|
||||
joinSession($session,$conn);
|
||||
#Check for user ID, if none $conn->disconnect("noid",generateUser());
|
||||
},
|
||||
utf8 => sub {
|
||||
my ($conn, $msg) = @_;
|
||||
my $sessionID = $conn->{"currentSession"}{"id"};
|
||||
my $messageData = "";
|
||||
eval { $messageData = from_json($msg) };
|
||||
if ($@) { $conn->send_utf8('{"error":1, "message":"ERROR: Invalid json"}'); return 0;}
|
||||
if ($messageData->{"type"} eq "message") {
|
||||
print "'".$messageData->{"message"}."' sent in session ".$sessionID."\n";
|
||||
for ($conn->server->connections) {
|
||||
if ($_->{"currentSession"}{"id"} == $sessionID) { $_->send_utf8(to_json($messageData)); }
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
)->start;
|
Loading…
x
Reference in New Issue
Block a user