websockets bufferedAmount example

// 10k max buffer size.
var THRESHOLD = 10240;
 
// Create a New WebSocket connection
var ws = new WebSocket("ws://w3mentor.com");
 
// Listen for the opening event
ws.onopen = function () {
 
   // Attempt to send update every second.
   setInterval( function() {
      // Send only if the buffer is not full
      if (ws.bufferedAmount < THRESHOLD) {
         ws.send(getApplicationState());
      }
   }, 1000);
};

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.