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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:14:17+00:00 2026-06-18T01:14:17+00:00

I am using the following websocket php class as my server: PHPWebSocket . It

  • 0

I am using the following websocket php class as my server: PHPWebSocket. It works well in every way I could have hoped for, but I’ve run into a bit of a problem.

I want to update the positions of connected clients (they can walk around) at a rate of maybe every 0.3 seconds. I figured it would be a simple matter of looking for the while loop and adding a heartbeat event there, but here it becomes tricky.

    // server state functions
function wsStartServer($host, $port) {
    if (isset($this->wsRead[0])) return false;

    if (!$this->wsRead[0] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) {
        return false;
    }
    if (!socket_set_option($this->wsRead[0], SOL_SOCKET, SO_REUSEADDR, 1)) {
        socket_close($this->wsRead[0]);
        return false;
    }
    if (!socket_bind($this->wsRead[0], $host, $port)) {
        socket_close($this->wsRead[0]);
        return false;
    }
    if (!socket_listen($this->wsRead[0], 10)) {
        socket_close($this->wsRead[0]);
        return false;
    }

    $this->log("Server starting");

    $write = array();
    $except = array();

    $nextPingCheck = time() + 1;
    while (isset($this->wsRead[0])) {

        $changed = $this->wsRead;
        $result = socket_select($changed, $write, $except, 1);
                     **beat()**
        if ($result === false) {
            socket_close($this->wsRead[0]);
            return false;
        }
        elseif ($result > 0) {
            foreach ($changed as $clientID => $socket) {
                if ($clientID != 0) {
                    // client socket changed
                    $buffer = '';
                    $bytes = @socket_recv($socket, $buffer, 4096, 0);

                    if ($bytes === false) {
                        // error on recv, remove client socket (will check to send close frame)
                        $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
                    }
                    elseif ($bytes > 0) {
                        // process handshake or frame(s)
                        if (!$this->wsProcessClient($clientID, $buffer, $bytes)) {
                            $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
                        }
                    }
                    else {
                        // 0 bytes received from client, meaning the client closed the TCP connection
                        $this->wsRemoveClient($clientID);
                    }
                }
                else {
                    // listen socket changed
                    $client = socket_accept($this->wsRead[0]);
                    if ($client !== false) {
                        // fetch client IP as integer
                        $clientIP = '';
                        $result = socket_getpeername($client, $clientIP);
                        $clientIP = ip2long($clientIP);

                        if ($result !== false && $this->wsClientCount < self::WS_MAX_CLIENTS && (!isset($this->wsClientIPCount[$clientIP]) || $this->wsClientIPCount[$clientIP] < self::WS_MAX_CLIENTS_PER_IP)) {
                            $this->wsAddClient($client, $clientIP);
                        }
                        else {
                            socket_close($client);
                        }
                    }
                }
            }
        }

        if (time() >= $nextPingCheck) {
            $this->wsCheckIdleClients();
            $nextPingCheck = time() + 1;
        }
    }

    return true; // returned when wsStopServer() is called
}

To me it doesn’t seem like there is any specific timer that says it should only run once per second, but it does. I am not talking about the ping check, even if I place my heartbeat call directly after the while (isset($this->wsRead[0])) {, it will only trigger once per second, and it has me completely stumped.

Am I looking in the wrong place? Is there something about PHP’s websocket implementation that slows down this while loop?

  • 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-18T01:14:19+00:00Added an answer on June 18, 2026 at 1:14 am

    You have a one second timeout in your socket_select call – that’s probably where your delay is introduced. If you want that call to be non-blocking, you can pass a zero for that timeout.

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

Sidebar

Related Questions

I am using following PHP class to display records. I also want to open
I am using following command for start the production server. nohup ruby script/server webrick
I have a site using SignalR(rc1) on IIS8/Windows Server 2012. It runs pretty ok,
Iam Using following code to run this event using IF CURRENT_TIME() = '23:50:00' But
I am using following form but when I am submitting form without entering any
I am using following configuration for sending mail through php mail function : function
I am following the following tutorial to construct a websocket: http://blueprintforge.com/blog/2012/02/13/websockets-in-play-2-dot-0 It works for
I wrote a websocket server in c++ that works fine with websocket protocol 00
I am using following code to connect to MySql Db but it gives error.
I'm using following model to store info about pages: class Page(models.Model): title = models.TextField(blank

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.