james28909 has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Monks! I am back again with another question. I am trying to establish a persistent connection to a gateway/websocket so i can add some functionality! I were trying to use REST::Client, but when the time come to do the connection/handshake with the actual gateway, i got something like "protocol not supported", which I took as REST::Client didnt support wss. I can connect to the api server all day long and read messages with REST::Client, but i cannot seems to post responses from my bot.

So this is where I started floundering around and copy/pasting code while not understanding it. Besides copy/pasting code, I believe IO::Socket would be a better approach to this, but I dont fully understand the whole server/client thing great either.

I would post some code but it would be the first failed attempt with the "protocol not supported" error.

To post code, I will post some psuedo code because I am not very sure as to what I need to do to actually make the request and read the responses with IO::Socket.

Do I need a server, or a client, or do I need a server and client? any help is very much appreciated :)

use strict; use warnings; use IO::Socket; use JSON; my $wss = 'ws://gateway'; my $client = IO::Socket->new($wss, 5000, tcp); #most examples use loca +lhost instead # do I do the handshake query here before the loop and then go to +the loop and start filtering # responses? while(1){ #filter responses }

I appreciate any hints of wisdom.

Replies are listed 'Best First'.
Re: Persistent connection to a websocket/gateway (wss://)
by tobyink (Canon) on May 16, 2017 at 17:19 UTC

    Mojolicious supports WebSocket. This example is copy-pasted from their documentation:

    use Mojo::UserAgent; use Mojo::IOLoop; # Open WebSocket to echo service my $ua = Mojo::UserAgent->new; $ua->websocket('ws://echo.websocket.org' => sub { my ($ua, $tx) = @_; # Check if WebSocket handshake was successful say 'WebSocket handshake failed!' and return unless $tx->is_websocke +t; # Wait for WebSocket to be closed $tx->on(finish => sub { my ($tx, $code, $reason) = @_; say "WebSocket closed with status $code."; }); # Close WebSocket after receiving one message $tx->on(message => sub { my ($tx, $msg) = @_; say "WebSocket message: $msg"; $tx->finish; }); # Send a message to the server $tx->send('Hi!'); }); # Start event loop if necessary Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
      I was able to get the response back using this code! I am so greatful because I would have never figured this much out. Now that i have a good starting point, I can see how it works. Now I think I just need to setup a timed interval to send back data, then the rest is down hill (hopefully, i may be back though haha!).
      I was more than able to get it to work. It did, however, take me a few days of learning http responses and headers a little bit, but I completed my goal, and am now building mucho functionality into the script.
Re: Persistent connection to a websocket/gateway (wss://)
by RonW (Parson) on May 19, 2017 at 21:54 UTC

    Net::Async::WebSocket::Client is another possibly, depending on what your "discord bot" needs to do (for example, it may need to query other resources on the Internet).

      edited*