Created
September 29, 2017 21:55
-
-
Save sgoguen/a1189819aced1ab788d2678e6df7ac2d to your computer and use it in GitHub Desktop.
Observable Webserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Object Watcher</title> | |
</head> | |
<body> | |
<div id="output"></div> | |
<!--Script references. --> | |
<!--Reference the jQuery library. --> | |
<script src="Scripts/jquery-2.0.3.min.js"></script> | |
<!--Reference the SignalR library. --> | |
<script src="Scripts/jquery.signalr-2.2.2.min.js"></script> | |
<!--Reference the autogenerated SignalR hub script. --> | |
<script src="http://localhost:8080/signalr/hubs"></script> | |
<!--Add script to update the page and send messages.--> | |
<script type="text/javascript"> | |
$(function () { | |
//Set the hubs URL for the connection | |
$.connection.hub.url = "http://localhost:8080/signalr"; | |
// Declare a proxy to reference the hub. | |
var chat = $.connection.myHub; | |
// Create a function that the hub can call to broadcast messages. | |
chat.client.clearOutput = function (html) { | |
// Add the message to the page. | |
$('#output').children().remove(); | |
}; | |
chat.client.updateOutput = function (html) { | |
// Add the message to the page. | |
$('#output').html(html); | |
}; | |
chat.client.appendOutput = function (html) { | |
return chat.client.updateOutput(html); | |
// Add the message to the page. | |
var element = $(html) | |
$('#output').append(element); | |
//$("#output").children().last().scrollTop(element.offset().top); | |
//console.log("Scroll top", $("#output").children().last().offset().top); | |
//$(window).scrollTop($("#output").children().last().offset().top); | |
}; | |
// Start the connection. | |
$.connection.hub.start().done(function () { | |
$('#output').text("Connected"); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() { | |
Util.CurrentQueryPath.Dump(); | |
string url = "http://localhost:8080"; | |
using (WebApp.Start(url, Configuration)) { | |
Task.Run(async () => { | |
//await Task.Delay(1000); | |
foreach (var x in Enumerable.Range(1, 10000)) { | |
await Task.Delay(10); | |
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); | |
var users = UserIds.Select(u => u.Key).ToArray(); | |
foreach (var userId in users) { | |
var client = context.Clients.Client(userId); | |
var html = Util.ToHtmlString(new { userId, users, x, sqr = x * x }); | |
client.appendOutput(html); | |
} | |
} | |
}); | |
Console.ReadLine(); | |
} | |
} | |
public void Configuration(IAppBuilder app) { | |
app.UseCors(CorsOptions.AllowAll); | |
app.MapSignalR(); | |
app.UseFileServer(new FileServerOptions { | |
EnableDefaultFiles = true, | |
EnableDirectoryBrowsing = true, | |
FileSystem = new PhysicalFileSystem(@"C:\Projects\LINQPad\Journal\2017-09\Misc\www\") | |
}); | |
} | |
public static ConcurrentDictionary<string, bool> UserIds = new ConcurrentDictionary<string, bool>(); | |
public class MyHub : Hub { | |
public override Task OnConnected() { | |
UserIds.AddOrUpdate(Context.ConnectionId, (u) => true, (u, b1) => true); | |
return base.OnConnected(); | |
} | |
public override Task OnDisconnected(bool stopCalled) { | |
bool value; | |
UserIds.TryRemove(Context.ConnectionId, out value); | |
return base.OnDisconnected(stopCalled); | |
} | |
public void Send(string name, string message) { | |
Clients.All.addMessage(name, message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment