Skip to content

Instantly share code, notes, and snippets.

@KrzysFR
Created October 28, 2013 17:13
Show Gist options
  • Save KrzysFR/7200767 to your computer and use it in GitHub Desktop.
Save KrzysFR/7200767 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.Owin.Hosting;
using Microsoft.AspNet.SignalR.Client;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client.Transports;
namespace Test.SignalR.Scaleout
{
public class Program
{
static void Main(string[] args)
{
string host = "127.0.0.1";
int port = 8081;
if (args.Length > 0)
{
port = Int32.Parse(args[0]);
}
string url = String.Format("http://{0}:{1}", host, port);
using (WebApp.Start<Startup>(url))
{
Console.WriteLine("Server running at " + url);
using(var cts = new CancellationTokenSource())
{
var workerTask = Task.Run(() => RunWorker(url, cts.Token), cts.Token);
Console.WriteLine("Press [ENTER] to stop");
Console.ReadLine();
cts.Cancel();
try { workerTask.GetAwaiter().GetResult(); }
catch { }
}
Console.WriteLine("Bye");
}
}
private static async Task RunWorker(string url, CancellationToken ct)
{
const int W = 800;
const int H = 600;
try
{
using (var hubConnection = new HubConnection(url))
{
hubConnection.TraceWriter = Console.Out;
hubConnection.TraceLevel = TraceLevels.All;
var proxy = hubConnection.CreateHubProxy("MouseTracking");
proxy.On<string, int, int>("move", (id, x, y) =>
{
Console.WriteLine("Received move event: " + id + ", " + x + ", " + y);
});
hubConnection.StateChanged += (_) => Console.WriteLine("Client: " + _.OldState + " => " + _.NewState);
hubConnection.Error += (x) => Console.Error.WriteLine("Client: " + x.ToString());
// wait for a bit, because the server may not have started yet
await Task.Delay(5000, ct);
Console.WriteLine("Connecting to " + hubConnection.Url);
await hubConnection.Start();
Console.WriteLine("Connected to hub!");
int cx = 300;
int cy = 300;
await proxy.Invoke("Join");
string callerId = proxy.GetValue<string>("id");
Console.WriteLine("> Joined as " + callerId);
var rnd = new Random();
while (!ct.IsCancellationRequested)
{
cx += rnd.Next(10) - 5;
cy += rnd.Next(10) - 5;
if (cx < 0) cx += W;
if (cy < 0) cy += H;
if (cx >= W) cx -= W;
if (cy >= H) cy -= H;
await proxy.Invoke("Move", cx, cy);
await Task.Delay(100 + rnd.Next(250), ct);
}
}
}
catch (Exception e)
{
Console.Error.WriteLine("FAILED: " + e.ToString());
throw;
}
}
}
}
using FoundationDB.SignalR.Scaleout;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Tracing;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
[assembly: OwinStartup(typeof(Test.SignalR.Scaleout.Startup))]
namespace Test.SignalR.Scaleout
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// TODO: change this to use a custom fdb.cluster file
string clusterFile = null; // "c:\path\to\you\fdb.cluster"
// Use FoundationDB for the Scaleout Backplane...
GlobalHost.DependencyResolver.UseFoundationDB(clusterFile, "DB", "Test/SignalR", "SignalR/Scaleout");
#if true
app.MapSignalR();
#else
app.Map("/signalr", map =>
{
var config = new HubConfiguration
{
EnableDetailedErrors = true,
// You can enable JSONP by uncommenting this line
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
map.RunSignalR(config);
});
#endif
//BackgroundThread.Start();
GlobalHost.Configuration.TransportConnectTimeout = TimeSpan.FromSeconds(30);
// Turn tracing on programmatically
GlobalHost.TraceManager.Switch.Level = SourceLevels.Verbose;
GlobalHost.TraceManager["Hello"].TraceInformation("goooooo");
//Console.WriteLine("SignalR configured");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment