Skip to content

Instantly share code, notes, and snippets.

@adam-stokes
Created February 12, 2014 02:29
Show Gist options
  • Save adam-stokes/8948917 to your computer and use it in GitHub Desktop.
Save adam-stokes/8948917 to your computer and use it in GitHub Desktop.
websocket4
-- Non-blocking request (https://10.0.3.1:17070)
-- Switching to non-blocking mode
-- Connect (https:10.0.3.1:17070)
-- Client >>> Server (https://10.0.3.1:17070)
GET / HTTP/1.1
Connection: Upgrade
Content-Length: 0
Host: 10.0.3.1:17070
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: IDg2NzkxMDkxNDU2MjA2MA==
User-Agent: Mojolicious (Perl)
Accept-Encoding: gzip
Upgrade: websocket
-- Client <<< Server (https://10.0.3.1:17070)
H
-- Client <<< Server (https://10.0.3.1:17070)
TTP/1.1 403 Forbidden
Mojo::Transaction::HTTP {
Parents Mojo::Transaction
public methods (8) : client_read, client_write, is_empty, keep_alive, previous, redirects, server_read, server_write
private methods (4) : _body, _headers, _start_line, _write
internals: {
connection "63b5fde9c19c9058d4d7fb31b2f7fe79",
events {
resume [
[0] sub { ... }
]
},
http_state "body",
local_address "10.0.3.1",
local_port 39911,
offset 0,
remote_address "10.0.3.1",
remote_port 17070,
req Mojo::Message::Request,
res Mojo::Message::Response,
state "finished",
write 0
}
}
WebSocket handshake failed!
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::Base -base;
use Mojo::UserAgent;
use Mojo::JSON qw(j);
use DDP;
my $params = j(
{ 'Type' => 'Admin',
'Request' => 'Login',
'RequestId' => 1,
'Params' => {
'AuthTag' => 'user-admin',
'Password' => '211fdd69b8942c10cef6cfb8a4748fa4'
}
}
);
my $ua = Mojo::UserAgent->new;
$ua->websocket(
'wss://10.0.3.1:17070' => sub {
my ($ua, $tx) = @_;
p $tx;
say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
$tx->on(
finish => sub {
my ($tx, $code, $reason) = @_;
say "WebSocket closed with status $code.";
}
);
$tx->on(
message => sub {
my ($tx, $msg) = @_;
say "WebSocket message: $msg";
$tx->finish;
}
);
$tx->send($params);
}
);
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
__END__
=pod
-- Non-blocking request (https://10.0.3.1:17070)
-- Switching to non-blocking mode
-- Connect (https:10.0.3.1:17070)
-- Client >>> Server (https://10.0.3.1:17070)
GET / HTTP/1.1
Accept-Encoding: gzip
Connection: Upgrade
Sec-WebSocket-Version: 13
Host: 10.0.3.1:17070
Content-Length: 0
Upgrade: websocket
User-Agent: Mojolicious (Perl)
Sec-WebSocket-Key: Mzg2NTk3Mzg1NDg3NjQ3Mg==
-- Client <<< Server (https://10.0.3.1:17070)
H
-- Client <<< Server (https://10.0.3.1:17070)
TTP/1.1 403 Forbidden
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment