-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintramine_websockets.pl
75 lines (65 loc) · 2.35 KB
/
intramine_websockets.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
66
67
68
69
70
71
72
73
74
75
# intramine_websockets.pl: a WebSockets server for IntraMine,
# using a single port. If any client sends a message, the message
# is sent out to all connected clients, which in turn decide
# what to do with the message.
# This is a WEBSOCKET server, which is a BACKGROUND server that uses
# the ws:// protocol rather than http:// for communication.
# Expects to receive and send strings only, no binary stuff.
#
# For details on use, see "Writing your own IntraMine server.txt#WebSockets"
# and the following section, "Writing your own IntraMine server.txt#IntraMine communications".
# See also Documentation/WS.html.
#
# This service is started by intramine_main.pl and needs no entry in data/serverlist.txt
# ( see "intramine_main.pl#LoadServerList()" ).
#
# Solo start without IntraMine (change to your path and preferred port number):
# perl C:\perlprogs\IntraMine\intramine_websockets.pl WEBSOCKETS WS 81 43140
use strict;
use warnings;
use utf8;
use Net::WebSocket::Server;
$| = 1;
my $page_name = shift @ARGV;
my $short_name = shift @ARGV;
my $mainPort = shift @ARGV; # Default 81
my $port_listen = shift @ARGV; # Default up over 42000
if (!defined($port_listen) || $port_listen !~ m!^\d+$!)
{
die("ERROR, no valid port number supplied to intramine_websockets.pl!");
}
ListenForWSConnections();
# Set up the WebSockets server, and on receiving a message
# rebroadcast to all listeners (including the original sender).
# Except for an exit message, for which just print good-bye and exit.
sub ListenForWSConnections {
my $MessageGuard = '_MG_';
# TEST ONLY
print("$short_name is listening on port |$port_listen|\n");
Net::WebSocket::Server->new
(
listen => $port_listen,
silence_max => 0, # or maybe try 30 (seconds)
on_connect => sub {
my ($serv, $conn) = @_;
# TEST ONLY
#print("WS on connect.\n");
$conn->on(
utf8 => sub {
my ($conn, $msg) = @_;
# TEST ONLY
#print("utf8: |$msg|\n");
if ($msg =~ m!$MessageGuard(FORCEEXIT|EXITEXITEXIT)$MessageGuard!)
{
print("WS EXIT bye!\n");
exit(0);
}
else
{
$_->send_utf8($msg) for $conn->server->connections;
}
}
);
},
)->start;
}