-
Notifications
You must be signed in to change notification settings - Fork 0
/
intramine_SSE.pl
65 lines (55 loc) · 2.45 KB
/
intramine_SSE.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# intramine_SSE.pl: a separate port for handling Server-Sent Events.
# On receiving a signal from Main (intramine_main.pl) call SendEventToClients()
# which will in turn send a Server-Sent Event to any web page that has registered with
# this server for SSEs.
# This is done on a separate port to avoid confusing a page content request with
# a Server-Sent Event.
# The hard work is done by swarmserver.pm#SendEventToClients().
# perl C:\perlprogs\mine\intramine_SSE.pl mainPort ourPort
use strict;
use warnings;
use utf8;
use Path::Tiny qw(path);
use lib path($0)->absolute->parent->child('libs')->stringify;
use common;
use swarmserver;
# Add modules to the above list as you need them.
#binmode(STDOUT, ":unix:utf8");
$| = 1;
my $PAGENAME = '';
my $SHORTNAME = '';
my $server_port = '';
my $port_listen = '';
SSInitialize(\$PAGENAME, \$SHORTNAME, \$server_port, \$port_listen);
my $kLOGMESSAGES = 0; # 1 == Log Output() messages
my $kDISPLAYMESSAGES = 0; # 1 == print messages from Output() to console window
# Log is at logs/IntraMine/$SHORTNAME $port_listen datestamp.txt in the IntraMine folder.
# Use the Output() sub for routine log/print.
StartNewLog($kLOGMESSAGES, $kDISPLAYMESSAGES);
Output("Starting $SHORTNAME on port $port_listen\n\n");
##### Put in %RequestAction entries to load JS and CSS and respond to requests.
my %RequestAction;
$RequestAction{'signal'} = \&HandleBroadcastRequest; # signal = anything, for here eg signal=activity
MainLoop(\%RequestAction);
####### subs for this server
# Main sends us an activity event: in response call SendEventToClients() in swarmserver.pm,
# which in turn sends out events to all browser clients that have registered for
# Server-Sent Events with "source = new EventSource(sourceURL);".
# See eg statusEvents.js, part of the Status server.
sub HandleBroadcastRequest {
my ($obj, $formH, $peeraddress) = @_;
if (defined($formH->{'signal'}))
{
if ($formH->{'signal'} ne '' && defined($formH->{'activeserver'}))
{
my $port = (defined($formH->{'port'})) ? $formH->{'port'}: '';
Output("Sending SSE $formH->{'signal'} to $formH->{'activeserver'}.\n");
# Over to swarmserver.pm#SendEventToClients(), arguments are eg
# SendEventToClients('activity', 'Search', port number); - 2nd arg is a server Short Name
SendEventToClients($formH->{'signal'}, $formH->{'activeserver'}, $port);
}
}
# Returned value is ignored by broadcaster
# - this is more of a "UDP" than "TCP" approach to communicating.
return('OK');
}