Implementation scheme of Web socket heartbeat package

Source: Internet
Author: User

Web Socket heartbeat Package Implementation 05/30/2010

Now that the network environment is complex, the socket heartbeat packet is an effective solution to get a healthy and strong connection, today we implement the heartbeat package in the web socket, yes, although we just do a simple chat room, but we make him stable and reliable a little bit is not wrong.

My heartbeat package is very simple, the principle is that the interval sends heartbeat packet data to the server, the server in a certain time to send back the heartbeat packet response, compared to the timeout limit, if the time exceeded the set timeout, it is considered that the current WebSocket connection with the server has been disconnected, close the current web socket connection, Aftercare, such as reconnecting, or pop-up tips ...

The more useful core code is this:

function KeepAlive (ws) {var time = new Date (); if (last_health! =-1 && (Time.gettime ()-Last_health > Health _timeout) {//At this point you can consider the connection to be disconnected, you can set the reconnection or close the connection $ ("#keeplive_box"). HTML ("server not Responding."). CSS ({"Color": "Red"});//ws.close ();} else{$ ("#keeplive_box"). HTML ("Connection OK"). css ({"Color": "Green"}), if (Ws.bufferedamount = = 0) {ws.send (' ~h#c~ ');}}}

This is the heartbeat function, which sends heartbeat packets and detects heartbeat health. We implanted it into the WebSocket OnOpen event to turn on heartbeat detection. Like this:

var ws = new WebSocket (to_url);  Ws.onopen=function () {$ ("#statustxt"). HTML ("connected."); $ ("#send_btn"). attr ("disabled", false); Heartbeat_timer = SetInterval (function () {keepalive (WS)}, 1000);}

Yes, this can be implemented in the Web socket heartbeat package, here are similar to multi-threaded send mode, while sending heartbeat packet, does not affect the normal data communication, our example is the last WebSocket chat room to improve, we can find that heartbeat package running process, will not interfere with our normal chat. Service side I also made some changes, similar principle, in more than a certain period of time without receiving the client's heartbeat packet, it is considered that the client has dropped, close the connection, recover resources.

Demo here: http://img.zendstudio.net/libs/websocket/chat/demo2.html

Now, there is a new problem, if we want to use a PHP socket to simulate the heartbeat packet technology, how to implement? Friends passing by, please give more advice, grateful.

| 3 Reviews Tags: html, HTML5, WebSocket

3 Reviews
    1. Sweet06/14/201013:45

      Hello! Code font is very comfortable, can you tell me what kind of font? I am using Zend Studio 5.5.1, can I change the code font in the article? Thank you!

      Reply
    2. Yahveh06/22/201021:39

      Virtually aggravating the burden of it, PHP socket efficiency is not very good
      WebSocket.class.php
      Private $Connection =array ();
      $c = new Connection;
      $c->sock = socket_accept ($this->master);
      if $buffer = = ' ~h#c~ ' $c->atime=time ()
      $Connection [] = $c;

      Then

      while ($true) {
      $time = time ();
      foreach ($Connection as $c)
      {
      if ($c->atime + sock);}
      }
      }

      Connection.class.php
      Class Connection
      {
      Public $sock;
      Public $atime = time ();
      }

      Should be able to understand it, do not know whether it is OK, tomorrow to test the next ...

      Reply
      • gently06/23/201009:58

        In order to be stable and reliable, this load sacrifice is necessary. PHP sockets currently belong to the toy level, I only use it for testing (BTW: In fact, our actual production also use PHP socket as a server, just a combination of C, so it is not entirely part of the PHP socket category). Pseudo-code meaning to see understand.

        Reply

Implementation scheme of Web socket heartbeat package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.