Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:25:57+00:00 2026-06-06T11:25:57+00:00

I have a very strange issue. I have an open WebSocket, the communication works

  • 0

I have a very strange issue. I have an open WebSocket, the communication works perfectly. I also have onblur and onfocus events for the window, which notify the server via said connection. However, in that case, the strings I receive are non-null-terminated. The communication works absolutely impeccably otherwise, even when sending the same strings that are sent on those respective blur-/focus-events. Why is that, and how can it be fixed?

Here’s some code:

$(document).ready(function(){

    initializeEverything();

    window.onblur = function(){ notifyFocusChange(false); };

    window.onfocus = function(){ notifyFocusChange(true); };

});

function notifyFocusChange(present){

    if(present){
        webSocket.send('presence:present');
    }else{
        webSocket.send('presence:absent');
    }

}

And here’s an example of a non-null-terminated string I receive when the events fire:

presence:absentÏ┘ê÷/à°äJ÷ÝÿLÓ▓ùM÷Ýÿ[

EDIT: It’s been suggested that it may be a server error, so here’s the code for decoding incoming messages:

private function decode($payload) {

    $length = ord($payload[1]) & 127;

    if ($length == 126) {
        $masks = substr($payload, 4, 4);
        $data = substr($payload, 8);
    } elseif ($length == 127) {
        $masks = substr($payload, 10, 4);
        $data = substr($payload, 14);
    } else {
        $masks = substr($payload, 2, 4);
        $data = substr($payload, 6);
    }

    $text = '';
    for ($i = 0; $i < strlen($data); ++$i) {
        $text .= $data[$i] ^ $masks[$i % 4];
    }

    return $text;

}

EDIT: It only happens in Chrome. I checked it on Firefox, which uses the same WebSocket-protocol, and everything works perfectly fine there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:25:59+00:00Added an answer on June 6, 2026 at 11:25 am

    The onblur and onfocus events are probably happening several times in rapid succession. Meaning that the websocket.send call is also being called quickly multiple times in rapid succession.

    If the multiple send calls happen close enough together then there is a high probability that you will receive multiple frames in a single read at the server and I believe that’s what you are seeing. In other words, the “garbage” in your payload is actually one or more subsequent WebSocket frames.

    You might be able to replicate the problem manually be doing two separate sends in the same Javascript context:

    function do_test () {
        ws.send("data1");
        ws.send("data2");
    }
    

    That will send two WebSocket frames close enough together that the server will probably read them all at once from the socket.

    In order to properly process frames you have to parse the payload length field and only read/unmask that specified amount of payload data. Anything remaining needs to be queued up as the beginning of a new frame.

    Also, not only can multiple Websocket frames be returned from one read from the socket, but you can’t rely on frames being read as whole frames: you might read half the frame in one read and the other half in the next frame. In other words, the first time you read from the socket you might get 3 whole websocket frames plus 1 byte of the 4th frame and then in the next read from the socket you get the rest of the 4th frame.

    This can generally be summed up this way: WebSockets are a message based transport protocol, while TCP sockets are a streaming protocol. Since WebSockets are layered on TCP, this means that the WebSocket server (and client for that matter) must do the translation to present whole messages to the application even though the underlying transport is not message based.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange issue in my WPF project. The main window contains
I have got a very strange input issue in which it seems that my
I have a very strange issue. My appplicaiton works fine on my PC (as
I have a very strange issue with the tableview: I have a table cell
I have a very strange issue on my hands. I have two IIS websites
I am facing a very strange issue. I have a SharePoint webpart that displays
I have a very strange issue that I'm hoping someone can help me with.
I have this very strange issue with my MVC 2 project. Often times, I'll
I have encountered a very strange issue. Whenever I use the .NET membership provider
I have ran into a very strange issue with Google Maps in Chrome 5.0.375.99:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.