public static function read_message($s)
 {
     $frame_type_str = socket_read($s, 1);
     $message = '';
     $frame_type = ord($frame_type_str);
     if (($frame_type & 0x80) == 0x80) {
         # The payload length is specified in the frame.
         # Read and discard
         $len = web_socket_protocol::payload_length($s);
         if ($len) {
             $message = socket_read($s, $len);
         }
     } else {
         # The payload is delimited with \xff.c
         $data = '';
         $chr = '';
         do {
             $chr = socket_read($s, 1);
             if ($chr != "ÿ") {
                 $data .= $chr;
             }
         } while ($chr && $chr != "ÿ");
         if ($frame_type == 0x0) {
             # I SHOULD ONLY ACCEPT THE DATA if the frame type is a null byte
             $message = $data;
         } else {
             # Discard data of other types.
             if (DEBUG) {
                 echo "[DEBUG] frame type is not a  null byte! TRASH\n";
             }
         }
     }
     return $message;
 }
Example #2
0
 public static function write($s, $data)
 {
     if ($GLOBALS['clients'][self::$current_socket_key] == 'websocket') {
         $data = web_socket_protocol::frame_response($data);
     }
     $bytes = @socket_write($s, $data, strlen($data));
     if ($bytes === false) {
         //client has disconnected
     }
 }
                        if (DEBUG) {
                            echo "[DEBUG] client {$sock} disconnecting...\n";
                        }
                        module::dispatch_event('close', false, $sock, array());
                        // close socket
                        socket_close($s);
                        unset($sockets[$sock]);
                        unset($clients[$sock]);
                        //array_splice($sockets, $sock, 1);
                        //array_splice($clients, $sock, 1);
                    } else {
                        if (!$clients[$sock] && web_socket_protocol::is_handshake($str)) {
                            if (DEBUG) {
                                echo "web socket request!\n";
                            }
                            response::me(web_socket_protocol::handshake($str));
                            $clients[$sock] = 'websocket';
                            //flag the client as a web socket client
                        } else {
                            ///TODO create client_data and
                            module::dispatch_event('message', $str, $sock, array());
                        }
                    }
                }
            }
        }
    }
} while (true);
socket_close($listen);
if ($listen_policy) {
    socket_close($listen_policy);