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

  • SEARCH
  • Home
  • 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 6876787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:31:16+00:00 2026-05-27T04:31:16+00:00

I’ve actually been working on this for the past few hours… I’ve overcome the

  • 0

I’ve actually been working on this for the past few hours… I’ve overcome the encoding and decoding of the messages.

However, I now face a difference problem. I can issue commands, however, the responses to each command are truncated to that of the length of the received string…

EXPECTED chat result: Oh dear, I’m 152
RECEIVED chat result: Oh

DEBUGGING FROM SERVER:

  2011-11-23 23:22:14 System: Socket Resource id #7 created.
  2011-11-23 23:22:14 System: Socket bound to 192.168.1.144:8000.
  2011-11-23 23:22:14 System: Start listening on Socket.
  2011-11-23 23:22:17 WebSocket: Resource id #9 CONNECTED!
  2011-11-23 23:22:17 WebSocket: Requesting handshake...
  2011-11-23 23:22:17 WebSocket: Key: 42MngFcIhXEKCLFloq6IYQ==
  2011-11-23 23:22:17 WebSocket: Accept: raRUiMJ6z2bTY6pDrOf7K4Q56Fc=
  2011-11-23 23:22:17 WebSocket: Origin: http://192.168.1.144
  2011-11-23 23:22:17 WebSocket: Handshaking...
  2011-11-23 23:22:17 WebSocket: Done handshaking...
  2011-11-23 23:22:18 WebSocket: RECEIVED BEFORE DECODE: ¿õ1ÆÞT
  2011-11-23 23:22:18 WebSocket: RECEIVED AFTER DECODE: age
  2011-11-23 23:22:18 WebSocket: <age
  2011-11-23 23:22:18 WebSocket: >Oh dear, I'm 152
  2011-11-23 23:22:18 WebSocket: SENT BEFORE ENCODE: Oh dear, I'm 152
  2011-11-23 23:22:18 WebSocket: SENT AFTER ENCODE: ¿õ1Æð

DEBUGGING FROM CLIENT:

  Socket Status: 0
  Socket Status: 1 (open)
  Sent: age
  Received: Oh

ENCODE and DECODE FUNCTIONS

function decode($msg) {

    $this->console("RECEIVED BEFORE DECODE: $msg");

    $len = $data = $decoded = $index = null;
    $len = $msg[1] & 127;

    if ($len === 126) {
        $this->masks = substr($msg, 4, 4);
        $data = substr($msg, 8);
        $this->initFrame = substr($msg, 0, 4);
    } else if ($len === 127) {
        $this->masks = substr($msg, 10, 4);
        $data = substr($msg, 14);
        $this->initFrame = substr($msg, 0, 10);
    } else {
        $this->masks = substr($msg, 2, 4);
        $data = substr($msg, 6);
        $this->initFrame = substr($msg, 0, 2);
    }
    for ($index = 0; $index < strlen($data); $index++) {
        $decoded .= $data[$index] ^ $this->masks[$index % 4];
    }

    $this->console("RECEIVED AFTER DECODE: $decoded");

    return $decoded;
}

function encode($msg) {

    $this->console("SENT BEFORE ENCODE: $msg");

    $index = $encoded = null;

    $len = strlen($msg);

    for ($index = 0; $index < $len; $index++) {
        $encoded .= $msg[$index] ^ $this->masks[$index % 4];
    }

    $encoded = $this->initFrame . $this->masks . $encoded;

    $this->console("SENT AFTER ENCODE: $encoded");

    return $encoded;
}
  • 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-05-27T04:31:17+00:00Added an answer on May 27, 2026 at 4:31 am

    So, finally found the problem. It was the encode/decode. Or should I say encode. It was encoding the message, but wasn’t following the frame protocol. Below you will find the new encode/decode functions. I did not create these, I found them in another source code, I just don’t remember where. lol

    public function decrypt(&$socket, &$message) {
        //$this->consoleLog("[DECRYPT][BEFORE][PLAIN]: $message");
        //$this->consoleLog("[DECRYPT][BEFORE][HEX]: " . bin2hex($message));
        $data = $message;
    
        $payloadLength = '';
        $mask = '';
        $unmaskedPayload = '';
        $decodedData = array();
    
        // estimate frame type:
        $firstByteBinary = sprintf('%08b', ord($data[0]));
        $secondByteBinary = sprintf('%08b', ord($data[1]));
        $opcode = bindec(substr($firstByteBinary, 4, 4));
        $isMasked = ($secondByteBinary[0] == '1') ? true : false;
        $payloadLength = ord($data[1]) & 127;
    
        // close connection if unmasked frame is received:
        if ($isMasked === false) {
            $this->disconnect($socket);
        }
    
        switch ($opcode) {
            // text frame:
            case 1:
                $decodedData['type'] = 'text';
                break;
    
            // connection close frame:
            case 8:
                $decodedData['type'] = 'close';
                break;
    
            // ping frame:
            case 9:
                $decodedData['type'] = 'ping';
                break;
    
            // pong frame:
            case 10:
                $decodedData['type'] = 'pong';
                break;
    
            default:
                // Close connection on unknown opcode:
                //$this->close(1003);
                break;
        }
    
        if ($payloadLength === 126) {
            $mask = substr($data, 4, 4);
            $payloadOffset = 8;
        } elseif ($payloadLength === 127) {
            $mask = substr($data, 10, 4);
            $payloadOffset = 14;
        } else {
            $mask = substr($data, 2, 4);
            $payloadOffset = 6;
        }
    
        $dataLength = strlen($data);
    
        if ($isMasked === true) {
            for ($i = $payloadOffset; $i < $dataLength; $i++) {
                $j = $i - $payloadOffset;
                $unmaskedPayload .= $data[$i] ^ $mask[$j % 4];
            }
            $decodedData['payload'] = $unmaskedPayload;
        } else {
            $payloadOffset = $payloadOffset - 4;
            $decodedData['payload'] = substr($data, $payloadOffset);
        }
    
    
    
        //$this->consoleLog("AFTER DECRYPTION: $decodedData");
    
        return $decodedData['payload'];
    }
    
    public function encrypt(&$socket, &$message, $type = 'text', $masked = true) {
    
        $payload = $message;
    
        $frameHead = array();
        $frame = '';
        $payloadLength = strlen($payload);
    
        switch ($type) {
            case 'text':
                // first byte indicates FIN, Text-Frame (10000001):
                $frameHead[0] = 129;
                break;
    
            case 'close':
                // first byte indicates FIN, Close Frame(10001000):
                $frameHead[0] = 136;
                break;
    
            case 'ping':
                // first byte indicates FIN, Ping frame (10001001):
                $frameHead[0] = 137;
                break;
    
            case 'pong':
                // first byte indicates FIN, Pong frame (10001010):
                $frameHead[0] = 138;
                break;
        }
    
        // set mask and payload length (using 1, 3 or 9 bytes) 
        if ($payloadLength > 65535) {
            $payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8);
            $frameHead[1] = ($masked === true) ? 255 : 127;
            for ($i = 0; $i < 8; $i++) {
                $frameHead[$i + 2] = bindec($payloadLengthBin[$i]);
            }
            // most significant bit MUST be 0 (close connection if frame too big)
            if ($frameHead[2] > 127) {
                $this->close(1004);
                return false;
            }
        } elseif ($payloadLength > 125) {
            $payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8);
            $frameHead[1] = ($masked === true) ? 254 : 126;
            $frameHead[2] = bindec($payloadLengthBin[0]);
            $frameHead[3] = bindec($payloadLengthBin[1]);
        } else {
            $frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
        }
    
        // convert frame-head to string:
        foreach (array_keys($frameHead) as $i) {
            $frameHead[$i] = chr($frameHead[$i]);
        }
        if ($masked === true) {
            // generate a random mask:
            $mask = array();
            for ($i = 0; $i < 4; $i++) {
                $mask[$i] = chr(rand(0, 255));
            }
    
            $frameHead = array_merge($frameHead, $mask);
        }
        $frame = implode('', $frameHead);
    
        // append payload to frame:
        $framePayload = array();
        for ($i = 0; $i < $payloadLength; $i++) {
            $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
        }
    
    
        //$this->consoleLog("[ENCRYPT][AFTER][PLAIN]: $frame");
        //$this->consoleLog("[ENCRYPT][BEFORE][AFTER]: " . bin2hex($frame));
    
    
        return $frame;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this

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.