Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created December 11, 2013 07:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeminutillo/7906274 to your computer and use it in GitHub Desktop.
Save mikeminutillo/7906274 to your computer and use it in GitHub Desktop.
Create a new Console App, Install all of the Packages (or enable package restore and hand-edit the packages.config file), Create a Web Folder and move your Scripts folder into it, Create Home.html in the root of /Web, mark all of the child files of /Web as Copy Always. Hit F5 and go to your browser for NancyFx, SignalR and static file hosting on…
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Nancy;
using Nancy.Owin;
using Owin;
using System;
using System.IO;
using System.Reflection;
public class Program
{
// The url where things are hosted
static string url = "http://localhost:9999";
static void Main(string[] args)
{
using(WebApp.Start<Startup>(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HubConfiguration { EnableDetailedErrors = true };
app.MapSignalR(config);
// Static Files are stored at /Web from the solution. JQuery and SignalR clients are stored there
var exeFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var webFolder = Path.Combine(exeFolder, "Web");
app.UseStaticFiles(webFolder);
app.UseNancy(opt =>
{
// If nancy cannot find a handler for the route then pass the request through the pipleine
opt.PassThroughWhenStatusCodesAre(HttpStatusCode.NotFound);
});
}
}
public class MyHub : Hub
{
// Very simple echo chat hub
public void Send(string message)
{
Clients.All.addMessage(message.Trim());
}
}
// Very simple Nancy Module
public class HomeModule : NancyModule
{
public HomeModule()
{
Get["/"] = _ => "Hello from Owin!";
}
}
<!DOCTYPE html>
<html>
<head>
<title>SignalR Self Host</title>
<script src="Scripts/jquery-2.0.3.js"></script>
<script src="Scripts/jquery.signalR-2.0.1.js"></script>
<script src="../signalr/hubs"></script>
</head>
<body>
<h1>SignalR Self Hosting</h1>
<div>
<div>
<textarea id="messagesText" rows="20" style="width:100%"></textarea>
</div>
<div>
<textarea id="newMessage" rows="3" style="width:100%"></textarea>
</div>
</div>
<script type="text/javascript">
var hub = $.connection.myHub,
$msgText = $("#messagesText"),
$newMessage = $("#newMessage");
hub.client.addMessage = function (message) {
$msgText.val($msgText.val() + "\r\n" + message);
}
$.connection.hub.start().done(function () {
console.log('connected: ' + $.connection.hub.id);
$newMessage.keyup(function (e) {
if ((e.keyCode || e.which) == 13) { // Enter keycode
hub.server.send($newMessage.val()).done(function () {
$newMessage.val('').focus();
});
return false;
}
});
}).fail(function(error){ console.log('Could not Connect! ' + error); });
</script>
</body>
</html>
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="jQuery" version="2.0.3" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR.Core" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR.JS" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.SignalR.Owin" version="1.1.4" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Owin.FileSystems" version="0.20-alpha-20220-88" targetFramework="net45" />
<package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Owin.StaticFiles" version="0.20-alpha-20220-88" targetFramework="net45" />
<package id="Nancy" version="0.21.1" targetFramework="net45" />
<package id="Nancy.Owin" version="0.21.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment