Skip to content

Instantly share code, notes, and snippets.

@bachwehbi
Created November 27, 2014 21:03
Show Gist options
  • Save bachwehbi/cf9270fbcec05f0fa712 to your computer and use it in GitHub Desktop.
Save bachwehbi/cf9270fbcec05f0fa712 to your computer and use it in GitHub Desktop.
Measures the latency of Beebotte using the C# client SDK
/////////////////////////////////////////////////////////////
// Copyright (c) 2013-2014 Beebotte <contact@beebotte.com>
// This program is published under the MIT License (http://opensource.org/licenses/MIT).
//
// This code uses the Beebotte API, you must have an account.
// You can register here: http://beebotte.com/register
//
// This program measures the latency of Beebotte real time service.
//
// Use the NuGet package manager console install dependencies:
// PM> Install-Package Beebotte.API.Client.Net
/////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Beebotte.API.Client.Net;
namespace bbt_latency
{
class Program
{
/* Set your account API access and secret keys */
static string accesskey = "YOUR_API_ACCESS_KEY";
static string secretkey = "YOUR_API_SECRET_KEY";
/* hostname */
static string hostname = "http://ws.beebotte.com";
/* Channel and resource names + access rights to request */
static string channelName = "YOUR_Channel"; //the channel name to subscribe to
static string resourceName = "YOUR_Resource"; //the resource to subscribe to
static bool isPrivateChannel = false; //Boolean indicating if the channel is private
static bool readAccess = true; //Boolean indicating if the connection has read access on the channel/resource
static bool writeAccess = true; //Boolean indicating if the connection has write access on the channel/resource
static int period = 2000; //period in milliseconds
static Connector connector;
static void Main(string[] args)
{
connector = new Connector(accesskey, secretkey, hostname);
connector.Connect();
connector.OnConnected += (u, m) =>
{
double milliseconds;
double received;
var subscription = connector.Subscribe(channelName, resourceName, isPrivateChannel, readAccess, writeAccess);
subscription.OnMessage += (i, n) =>
{
milliseconds = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
received = Convert.ToDouble(n.Message.data);
Console.WriteLine(milliseconds - received);
};
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = period;
aTimer.Enabled = true;
};
System.Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
var milliseconds = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
connector.Publish(channelName, resourceName, isPrivateChannel, milliseconds.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment