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 7986189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:46:57+00:00 2026-06-04T11:46:57+00:00

Update : I solved the decoding problem, thanks to pimvdb Follows the solution (in

  • 0

Update: I solved the decoding problem, thanks to pimvdb

Follows the solution (in PHP):

$len = $masks = $data = $decoded = null;

$len = ord ($buffer[1]) & 127;

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

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

*** Begin of the original topic ***

I’m trying to develop HTML5 WebSocket for a personal application, using hybi-17 handshake.

I’m started with phpwebsocket and the only thing I changed is the handshake function, from the original to this:

function dohandshake($user,$buffer){
  $key = null;
  
  console("\nRequesting handshake...");
  console($buffer);
  console("Handshaking...");
  
  preg_match ("#Sec-WebSocket-Key: (.*?)\r\n#", $buffer, $match) && $key = $match[1];
  
  $key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  $key = sha1 ($key);
  $key = pack ('H*', $key);
  $key = base64_encode ($key);
  
  $upgrade = 
    "HTTP/1.1 101 Switching Protocols\r\n" .
    "Upgrade: websocket\r\n" .
    "Connection: Upgrade\r\n" .
    "Sec-WebSocket-Accept: {$key}\r\n\r\n";
  
  socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
  $user->handshake=true;
  console($upgrade);
  console("Done handshaking...");
  return true;
}

I used phpwebsocket source code, both for the client (http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/client.html) and the server (http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/server.php).

At this point, I tested the application. Follows the server debug:

Server Started : 2011-10-30 13:45:41
Master socket  : Resource id #4
Listening on   : localhost port 12345

Resource id #5 CONNECTED!

Requesting handshake...
GET /phpwebsocket/server.php HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:12345
Sec-WebSocket-Origin: http://localhost
Sec-WebSocket-Key: +S/J2jcp/UKIS1HTW0n1/w==
Sec-WebSocket-Version: 8


Handshaking...
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: LEULWidwXDxY02iv3O+xksrxFz4=


Done handshaking...
< ��z�}p
> ��z�}p not understood
Resource id #5 DISCONNECTED!  

And this is the client debug:

WebSocket - status 0
Welcome - status 1
Sent: hello
Disconnected - status 3

What I did was simply connect the client and send the command ‘Hello’.
As you can see, server received encoded data and not plaintext: why?

I’m looking for someone who helps me to develop html5 websocket with hybi-17.

Cyaz

PS: this (Implementing handshake for hybi-17) topic and this one (Decoding network chars (HTML5 Websocket)) might help.

PPS: I tested on Chromium 15.0.874.106~r107270-0ubuntu0.11.04.1

  • 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-04T11:46:59+00:00Added an answer on June 4, 2026 at 11:46 am

    Decode solution (thanks to @pimvdb):

    $len = $masks = $data = $decoded = null;
    
    $len = ord ($buffer[1]) & 127;
    
    if ($len === 126) {
      $masks = substr ($buffer, 4, 4);
      $data = substr ($buffer, 8);
    }
    else if ($len === 127) {
      $masks = substr ($buffer, 10, 4);
      $data = substr ($buffer, 14);
    }
    else {
      $masks = substr ($buffer, 2, 4);
      $data = substr ($buffer, 6);
    }
    
    for ($index = 0; $index < strlen ($data); $index++) {
      $decoded .= $data[$index] ^ $masks[$index % 4];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
UPDATE: First problem solved, second one described at the bottom of this post. UPDATE2:
Update II Problem Solved but Why? This has been the biggest headache ever. My
[Update] Solved the Problem by generating a new web.config. Possible error was the second
update SOLVED - the problem is not with my code. there is a design
UPDATE SOLVED - Thank's to @MusiGenesis persistence with this I solved the problem by
SOLVED : The latest update solved the problem somehow. I am experiencing a very
UPDATE: SOLVED Hi all, i've got it, just save cookie to temp file, and
UPDATE The issue is solved, all the code you can see works. Hello! I
Updated Problem solved, I have some design problem here. The directory looks like that:

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.