Connect perl script to a websocket (using mojolicious) - perl

Here is my issue : I have got an API using mojolicious, an external script perl and a JS file, and I would like to connect them this way: the external script launch a random POST request, if it is a success it have to send the message "Success" throught a websocket. If an error occures, It will have to send "Error". The websocket on the API will just relay the message for the JS which will use it.
How I imagine the code to be :
Inside the Mojolicious launcher script:
websocket '/foo' => sub {
$self->on(message => sub {
my ($self, $msg) = #_;
$self->send($msg);
});
};
when get a message send it
Inside the JS file:
var ws = new WebSocket('ws://api/foo');
ws.onmessage = function(msg){
if(msg == "Error") {console.log("got an error")};
else if(msg == "Success") {console.log("got a success")};
};
So, how can I connect my external script to the websocket, and be able to send "Error" or "Success"? (This external script has nothing to do with the web server, it s somewhere else, doing something else).

I understand that you want a plain perl-script who connects to the Mojolicious webserver where you have a websocket endpoint.
The external perl script should connect to the websocket server and send some messages. The websocket server can then redistribute those messages to other clients.
Check my github where you can find the above screnario.
Perl Mojolicious websockets

Related

Net::XMPP Perl module: Can this be used to check for chat messages on server?

I can now connect to my jabber server using the perl module Net::XMPP. Now I would like to know: Is it possible to monitor the server for incoming chat messages (NOT email messages) and act accordingly if one is recieved?
The basic code I currently have is:
#!/bin/perl -w
use strict;
use warnings;
use Net::XMPP;
my $con = new Net::XMPP::Client();
my $status = $con->Connect(
hostname => 'hostnamepart',
connectiontype => 'tcpip',
tls => 1,
ssl_ca_path =>'path for cert');
die('ERROR: XMPP connection failed') if ! defined($status);
The $status variable returns 1 when I run the code above which I assume means that I connected ok. However, now I am stuck! I have looked through the online documentation on the metacpan.org website for Net::XMPP but cannot figure out if it's even possible to do what I want. Any help appreciated.

Perl WWW::Mechanize for HTTPS over proxy

I am trying to scrape a website using WWW::Mechanize module. I have configured the mechanize agent with a proxy URL and setting the proxy credentials using credentials method.
Code snippet :
my $url = 'https://abcde.com';
my $proxy_username = 'abc';
my $proxy_password = 'xyz';
my $proxy_url = 'http://xx.xxx.xxx.xxx:13228';
my $mechanize_agent = new WWW::Mechanize('cookie_jar' => {}, 'noproxy' => 1, 'ssl_opts' => { 'verify_hostname' => 0 });
$mechanize_agent->credentials( $proxy_username, $proxy_password );
$mechanize_agent->proxy(['http', 'https'], $proxy_url);
$mechanize_agent->get($url) or die 'Error in get request of $url: $#';
When URL is a plain HTTP, the script fetches and gives back the result. But when I try to hit HTTPS url I get the error
establishing SSL tunnel failed: 407 Proxy Authentication Required
I credentials are valid and I can view the website using proxy URL in Mozilla browser. Also i need t avoid using call to env_proxy() function since the proxy URL is dynamic. How can I get let my script fetch HTTPS request?
All suggestions are welcome!
thanks in advance.

Simple SMPP Perl Script

I need your help/advices on my very short script I wrote in Perl in order to send SMS via SMPP protocol.
I got a SMS gateway which is perfectly working (sending SMS via HTML request or via web interface works), let's say this gateway has IP 192.168.1.15.
Its SMPP service is listening to TCP 2775 (I can successfully telnet to 2775, so I guess SMPP service is working on my SMS gateway)
my $smpp = Net::SMPP->new_transmitter(192.168.1.15,
port=>2775,
system_id =>"administrator",
password =>"passwdexample") or die;
$resp_pdu = $smpp->submit_sm(destination_addr => '+400123456789',
short_message => 'test message') or die;
die "Response indicated error: " . $Resp_PDU->explain_status() if $resp_pdu->status;
When I run the script, here the error I got :
Response indicated error: Incorrect BIND Status for given command (ESME_RINVBNDS TS=0x00000004) at C:\temp\smpptest.pl line .
Unfortunately, I haven't find so much help on internet, but according to this link : SMPP Errors Codes It says :
You must bind first before any other request is handled.
However, my bind is done with new_transmitter, and I don't get any errors at this point, so I don't understand how it cannot bind the TCP session (my credentials are corrects, I tried that in a telnet session).
That's the first time I use such a plugin, so maybe I'm missing something, and maybe someone has already met this error !
Many thanks for your help :-)
Try to quote the IP address:
Net::SMPP->new_transmitter("192.168.1.15", ...);

deploying cgi to psgi converted application in apache

#!C:/perl/bin/perl.exe
use CGI;
my $q = CGI->new;
print $q->header('text/plain'),
"Hello ", $q->param('name');
#CONVERTED PSGI PAGE
#!C:/perl/bin/perl.exe
use CGI::PSGI;
my $app = sub {
my $env = shift;
my $q = CGI::PSGI->new($env);
return [
$q->psgi_header('text/plain'),
[ "Hello ", $q->param('name') ],
];
};
I run this cgi.pl in apache server as
http://localhost/cgi-bin/cgi.pl
but I cant able to run the converted psgi.pl in apache server
its displaying
please help
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#example.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log.
CGI and PSGI are two different specifications of how a web server and an external program communicate.
Under CGI, the web server expects to receive text output from the program, consisting of the HTTP Response headers, a blank line, and the HTML rendered by the program.
The CGI module implements this logic for the apache server, and if the output from the program does not comply, apache reports the 500 error.
Under PSGI, the web server expects the program to return a three element list consisting of the HTTP response code, the HTTP Response headers, and the HTML rendered by the program.
So you can see that a program conforming to the PSGI spec would confuse the mod_cgi.
So you need to install an apache module that implements PSGI, or employ a Perl module (the CGI::PSGI docs suggest CGI::Emulate::PSGI ) that will accept your PSGI list and convert to CGI for you.

500 SSL read timeout error in LWP perl

I have a perl script which will post HTTP request to specified server URL (Say: http://some-ip/here_action_url ). My problem is, Sometimes I am getting the below error.
Error:
500 SSL read timeout.
Sample Code:
my $ua = new LWP::UserAgent;
$ua->timeout(30);
my $res = $ua->post( $url, { 'data' => $my_data } );
if(! $res->is_success ) {
# Error Logging
print $res->status_line."\n";
}
else {
$response_content = $res->content;
}
I read about the error. Most of the documents are saying that it is because of the response delay in server side.
I just want to confirm, whether this error is coming because of server response delay? (or) Can be the problem with my perl script?
If you get a result some of the time, and the error at other times, then it looks like your code is working.
If you alway get the 500 error, it indicates a connection problem. Would need to know more about the service you are trying to connect to, does it require certificates or other authentication (which may be needed for secure socket layer connection)