/* Copyright 2004 Macromedia, Inc. All rights reserved. The following is Sample Code and is subject to all restrictions on such code as contained in the Macromedia Flash Communication Server MX 1.5 End User License Agreement . */ application.onConnect = function(p_client, p_autoSenseBW) { //Add security here this.acceptConnection(p_client); if (p_autoSenseBW) { this.calculateClientBw(p_client); } else { p_client.call("onBWDone"); } }; Client.prototype.getStreamLength = function(p_streamName) { return Stream.length(p_streamName); }; Client.prototype.checkBandwidth = function() { application.calculateClientBw(this); }; application.calculateClientBw = function(p_client) { p_client.payload = new Array(); for (var i = 0; i<1200; i++) { p_client.payload[i] = Math.random(); //16K approx } var res = new Object(); res.latency = 0; res.cumLatency = 1; res.bwTime = 0; res.count = 0; res.sent = 0; res.client = p_client; var stats = p_client.getStats(); var now = (new Date()).getTime()/1; res.pakSent = new Array(); res.pakRecv = new Array(); res.beginningValues = {b_down:stats.bytes_out, b_up:stats.bytes_in, time:now}; res.onResult = function(p_val) { var now = (new Date()).getTime()/1; this.pakRecv[this.count] = now; //trace( "Packet interval = " + (this.pakRecv[this.count] -this.pakSent[this.count])*1 ); this.count++; var timePassed = (now-this.beginningValues.time); if (this.count == 1) { this.latency = Math.min(timePassed, 800); this.latency = Math.max(this.latency, 10); } //trace("count = " + this.count + ", sent = " + this.sent + ", timePassed = " + timePassed); // If we have a hi-speed network with low latency send more to determine // better bandwidth numbers, send no more than 6 packets if (this.count == 2 && (timePassed<2000)) { this.pakSent[res.sent++] = now; this.cumLatency++; this.client.call("onBWCheck", res, this.client.payload); } else if (this.sent == this.count) { // See if we need to normalize latency if (this.latency>=100) { // make sure we detect sattelite and modem correctly if (this.pakRecv[1]-this.pakRecv[0]>1000) { this.latency = 100; } } delete this.client.payload; // Got back responses for all the packets compute the bandwidth. var stats = this.client.getStats(); var deltaDown = (stats.bytes_out-this.beginningValues.b_down)*8/1000; var deltaTime = ((now-this.beginningValues.time)-(this.latency*this.cumLatency))/1000; if (deltaTime<=0) { deltaTime = (now-this.beginningValues.time)/1000; } var kbitDown = Math.round(deltaDown/deltaTime); trace("onBWDone: kbitDown = "+kbitDown+", deltaDown= "+deltaDown+", deltaTime = "+deltaTime+", latency = "+this.latency+"KBytes "+(stats.bytes_out-this.beginningValues.b_down)/1024); this.client.call("onBWDone", null, kbitDown, deltaDown, deltaTime, this.latency); } }; res.pakSent[res.sent++] = now; p_client.call("onBWCheck", res, ""); res.pakSent[res.sent++] = now; p_client.call("onBWCheck", res, p_client.payload); };