Unable to establish WebSocket connection

After thorough study of RFC 6455, I am perplexed as to why the handshake with the server keeps failing. Despite checking the communication with the Firefox add-on "HTTP Live Headers" and finding everything in order...

I conducted tests using FireFox.

For the Server/Perl:

use IO::Socket::INET;
use Digest::SHA1 qw(sha1_base64);

$| = 1;

my $sock = IO::Socket::INET->new(LocalPort=>6060, Listen=>1, ReuseAddr=>1); 

while(my $client = $sock->accept) {

    my $key = undef;

    sysread $client, my $buf, 10000;

    while($buf =~s/(.*)\r\n//) {
        my $line = $1; 
        print "line='$line'\n";
        
        if($line =~/^Sec\-WebSocket\-Key:\s+(.*)$/i) {
            $key = $1; 
         }
     }

     $key .= '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
     my $return_key = sha1_base64($key);
     
     print $client "HTTP/1.1 101 Switching Protocols\r\n"; 
     print $client "Upgrade: websocket\r\n"; 
     print $client "Connection: Upgrade\r\n";
     print $client "Sec-WebSocket-Accept: $return_key\r\n"; 
     print $client "\r\n";
}

Client/JavaScript

if("WebSocket" in window ) {
    sock = new WebSocket("ws://localhost:6060"); 
    sock.onopen = function() { /*this never fires*/ };
    sock.onerror = function() { /*the problem: this always fires*/ };
}

Answer №1

There seems to be an issue with the padding in Perl's sha1_base64 function as it is not including the necessary "=" at the end. Simply adding a single "=" should resolve this:

my $return_key = sha1_base64($key) . "=";

Perhaps exploring alternative implementations for sha1/base64 would be worth considering?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract certain attributes from a Json file and create a new Json dataset

My WordPress website has an API that exports posts as a Json file. I am working on a new website that will use this data, but I need to reformat the Json file with my custom property names and exclude some properties. Here is an example of the Json output ...

Using AngularJS to create CSS animations with @keyframes inside a directive is an innovative way

I'm facing a challenge with creating @keyframes animation CSS within an AngularJS directive function. The issue is that I need to use a variable from the scope to generate these keyframes, but I am unsure of how to retrieve it. app.directive("myCSSDi ...

Fixed Positioning Div to Stay at the Top while Scrolling

Currently, I have successfully implemented the functionality to stick the div to the top once it scrolls down by 320px. However, I am curious if there is an alternative approach to achieving this effect. Below is the code snippet I am using: jQuery(functi ...

The error message you are encountering is: "Error: Unable to find function axios

Can't figure out why I'm encountering this error message: TypeError: axios.get is not functioning properly 4 | 5 | export const getTotalPayout = async (userId: string) => { > 6 | const response = await axios.get(`${endpoint}ge ...

Begin a new countdown timer upon clicking the next button

Currently, I am developing a bid auction website and I have implemented a countdown timer script. The timer works perfectly on the initial window load. However, when I click on the restart button, it fails to reset the countdown timer to a new value. < ...

Unable to load custom package in Angular 2 testing environment

I've been following the official Angular 2 testing guide on an existing project. Everything runs smoothly when I use my custom library, downloadjs, in the application. However, I encounter an error in the console during test execution: "__zone_symbol ...

What is the most effective way to loop and render elements within JSX?

Trying to achieve this functionality: import React from 'react'; export default class HelloWorld extends React.Component { public render(): JSX.Element { let elements = {"0": "aaaaa"}; return ( ...

@HostBinding in Angular 2 does not trigger change detection

When using @HostBinding in conjunction with Chrome's Drag and Drop API, the code looks like this: @Directive({ selector: '[sortable-article]' }) export class SortableArticleComponent { @HostBinding('class.dragged-element') ...

Preserving user interaction history in the browser while syncing with the server in AngularJS

My current challenge involves handling an HTML form that is connected to an object named a through an ngModel. When a user updates the data in the form, I send a PUT request to update the resource on the server. The response from the server contains update ...

An interesting approach to utilizing toggle functionality in JQuery is by incorporating a feature that automatically closes the div when

Currently, I am utilizing JQuery's toggle function to slide a ul li element. However, my desired functionality is for the div to close if someone clicks outside of it (anywhere on the page) while it is in the toggle Down condition. Below, you'll ...

The tooltip being displayed is plain and lacks any design elements

When I hover over my a element, only a simple tooltip appears without any styling, unlike what is shown in the Bootstrap documentation. (I am creating the a element using JavaScript) HTML <!DOCTYPE html> <html lang="en"> <head> ...

Dynamic visual content (map)

I'm currently working on creating an interactive image for my website where clicking on points A, B, C, ... N will reveal bubbles with images and text inside them. Would anyone be able to provide guidance on how to achieve this? Or direct me to resou ...

What is the process for comprehending an event that is not triggered by the task queue within the event loop specifications?

According to the guidelines: Various events are triggered through tasks apart from just the task queue. I am curious to understand what exactly is meant by "various" and the specific types of tasks being referred to here? ...

Using Javascript to make an AJAX request and appending "?=####" to the end of the call

When I make an ajax call to a URL on my server, the response from my logs shows as "/action?_=1423024004825". Is there a way to remove this extra information? $.ajax({ type: "GET", url: "/action" }); ...

What are some methods to conceal an email address using Javascript?

let user = 'alex'; let domain = 'gmail.com'; let send = 'msg'; document.getElementById("email").href = "ma" + send + "ilto:" + user + "@" + domain; <a id="email"> <img src="imgs/pic.jpg"> </a> I have been w ...

Activate the zoom feature in jquery mobile

I am looking to implement the standard zooming effect in jquery mobile for my iOS iPhone app using jqm 1.3.2. After attempting the following: <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, user-s ...

Displaying elements of array in Pug template is a key task for

As a newcomer to the Jade/Pug template engine used in Express, I am faced with a challenge. I need to display the name property of each object within an associative array that is passed as a parameter to my pug template from an express route module. I hav ...

Changing column layouts on mobile using Bootstrap 4

Using Bootstrap's grid system, I have created a layout with a sidebar. The structure is as follows: <div class="row"> <div class="col-md-3"> ... </div> <div class="col-md-9"> <div class="row"> <div ...

Tips for Deleting Navigation History in Nativescript-Vue Manual Routing

I find myself in a situation where the navigation calls are stacking up continuously, as illustrated in the image below. https://i.sstatic.net/evlFx.png This poses a problem, especially when I try to logout as it only adds another entry to the navigation ...

The process of invoking a function within another function in TypeScript Angular

Just starting out with Angular 2, I've written the following code in my Angular project: export class TestClass { constructor() { this.initMap(); } initMap() { this.marker.addListener('dragend', this.onMarkerDr ...