Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Last active December 15, 2015 08:49
Show Gist options
  • Save panesofglass/5233731 to your computer and use it in GitHub Desktop.
Save panesofglass/5233731 to your computer and use it in GitHub Desktop.
Use SignalR from F# via OWIN
(function (document, $, undefined) {
"use strict";
$(document).ready(function () {
var tryFS;
$.connection.hub.url = 'http://localhost:8081/signalr';
tryFS = $.connection.tryFS;
if (tryFS) {
// Define the `addMessage` function on the client.
tryFS.client.addMessage = function (value) {
$('#results').append('<p>' + value + '</p>');
console.log('Server called addMessage(' + value + ')');
};
// Kick off the hub.
$.connection.hub.start().
done(function () {
$('#submit').on('click', function () {
tryFS.server.send($('#source').val());
});
});
}
else {
console.log('No hub found by the name of tryFS');
}
});
})(document, jQuery);
namespace tryfs
open System
open Owin
open Microsoft.AspNet.SignalR
open Microsoft.Owin.Hosting
// Use ImpromptuInterface for dynamic support in the TryFS.Send method.
// Could also use http://fssnip.net/2U or http://fssnip.net/2V
open ImpromptuInterface.FSharp
// Define a Hub
type TryFS() =
inherit Hub()
member x.Send(message: string) : unit =
x.Clients.All?addMessage(message)
// Create a Startup class with a Configuration member taking an `Owin.IAppBuilder`.
type Startup() =
member x.Configuration(app: Owin.IAppBuilder) =
let config = new HubConfiguration(EnableCrossDomain = true)
app.MapHubs(config) |> ignore
module Program =
[<EntryPoint>]
let main args =
let url = "http://localhost:8081/"
let disposable = WebApplication.Start<Startup>(url)
Console.WriteLine("Server running on " + url)
Console.WriteLine("Press Enter to stop.")
Console.ReadLine() |> ignore
disposable.Dispose()
0 // completed successfully
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>SignalR + F#</title>
</head>
<body>
<input type="text" id="source" />
<button type="button" id="submit" value="submit">Send</button>
<div id="results"></div>
<script src="Scripts/jquery.signalR-1.0.1.min.js"></script>
<script src="http://localhost:8081/signalr/hubs" type="text/javascript"></script>
<script src="app/app.js"></script>
</body>
</html>
@panesofglass
Copy link
Author

Such a simple fix: specify the return type of Send. Otherwise, addMessage returns 'a, which caused the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment