【发布时间】:2014-07-25 15:17:15
【问题描述】:
我有一个关于应该如何使用 IO::Socket 的问题;我有一个应该不断运行的脚本,监视 Asterisk 服务器的某些事件。当这些事件发生时,脚本通过 TCP 套接字将事件中的数据发送到另一台服务器。我发现偶尔,套接字会关闭。我的问题是我是否应该使用单个套接字并使其永远打开(并弄清楚为什么 + 阻止它关闭),还是应该为发送的每一位数据打开和关闭一个新套接字?
我在这类事情上的经验非常少,而且我已经阅读了所有文档,但没有找到我正在寻找的答案。以下是我到目前为止所获得的示例:
#!/usr/bin/perl
use Asterisk::AMI;
use IO::Socket;
use strict;
use warnings;
my $sock = new IO::Socket::INET (
PeerAddr => '127.0.0.1',
PeerPort => '1234',
Proto => 'tcp',
);
sub newchannel {
my ($ami, $event) = @_;
if ($event->{'Context'} eq "from-trunk") {
my $unique_id = $event->{'Uniqueid'};
my $this_call = $call{$unique_id};
$this_call->{caller_name} = $event->{'CallerIDName'};
$this_call->{caller_number} = $event->{'CallerIDNum'};
$this_call->{dnis} = $event->{'Exten'};
$call{$unique_id} = $this_call;
};
}
sub ringcheck {
my ($ami, $event) = @_;
if ($event->{SubEvent} eq 'Begin') {
my $unique_id = $event->{UniqueID};
if (exists $call{$unique_id}) {
my $this_call = $call{$unique_id};
$this_call->{system_extension} = $event->{Dialstring};
$this_call->{dest_uniqueid} = $event->{DestUniqueID};
printf $sock "R|%s|%s|%s||%s\n",
$this_call->{caller_name},
$this_call->{caller_number},
$this_call->{system_extension},
$this_call->{dnis};
$this_call->{status} = "ringing";
}
}
除此之外还有更多内容,但这表明我觉得我应该在哪里开始/停止一个新的套接字(在 ringcheck 子中)。
如果您需要我澄清或添加任何内容,请告诉我。
谢谢!
【问题讨论】: