# Fable.SignalR [![Nuget](https://img.shields.io/nuget/v/Fable.SignalR.svg?maxAge=0&colorB=brightgreen&label=Fable.SignalR)](https://www.nuget.org/packages/Fable.SignalR)
Fable bindings for the SignalR client, a wrapper for the .NET client,
and ASP.NET Core/Giraffe/Saturn wrappers for SignalR server hubs.
The full documentation can be found [here](https://shmew.github.io/Fable.SignalR/).
### On the client
```fsharp
let textDisplay = React.functionComponent(fun (input: {| count: int; text: string |}) ->
React.fragment [
Html.div input.count
Html.div input.text
])
let buttons = React.functionComponent(fun (input: {| count: int; hub: Hub<Action,Response> |}) ->
React.fragment [
Html.button [
prop.text "Increment"
prop.onClick <| fun _ -> input.hub.current.sendNow (Action.IncrementCount input.count)
]
Html.button [
prop.text "Decrement"
prop.onClick <| fun _ -> input.hub.current.sendNow (Action.DecrementCount input.count)
]
Html.button [
prop.text "Get Random Character"
prop.onClick <| fun _ -> input.hub.current.sendNow Action.RandomCharacter
]
])
let render = React.functionComponent(fun () ->
let count,setCount = React.useState 0
let text,setText = React.useState ""
let hub =
React.useSignalR<Action,Response>(fun hub ->
hub.withUrl(Endpoints.Root)
.withAutomaticReconnect()
.configureLogging(LogLevel.Debug)
.onMessage <|
function
| Response.NewCount i -> setCount i
| Response.RandomCharacter str -> setText str
)
Html.div [
prop.children [
textDisplay {| count = count; text = text |}
buttons {| count = count; hub = hub |}
]
])
```
### On the server
```fsharp
module SignalRHub =
open FSharp.Control.Tasks.V2
let update (msg: Action) =
match msg with
| Action.IncrementCount i -> Response.NewCount(i + 1)
| Action.DecrementCount i -> Response.NewCount(i - 1)
| Action.RandomCharacter ->
let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
System.Random().Next(0,characters.Length-1)
|> fun i -> characters.ToCharArray().[i]
|> string
|> Response.RandomCharacter
let invoke (msg: Action) _ =
task { return update msg }
let send (msg: Action) (hubContext: FableHub<Action,Response>) =
update msg
|> hubContext.Clients.Caller.Send
application {
use_signalr (
configure_signalr {
endpoint Endpoints.Root
send SignalRHub.send
invoke SignalRHub.invoke
}
)
...
}
```
### The shared file
```fsharp
[<RequireQualifiedAccess>]
type Action =
| IncrementCount of int
| DecrementCount of int
| RandomCharacter
[<RequireQualifiedAccess>]
type Response =
| NewCount of int
| RandomCharacter of string
module Endpoints =
let [<Literal>] Root = "/SignalR"
```
SignalR和Fable的功能类型安全包装器。___下载.zip
版权申诉
148 浏览量
2023-04-26
11:28:30
上传
评论
收藏 433KB ZIP 举报
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
最新资源
- Pytorch框架基于Bert构建词向量+Bi-LSTM+Attention实现情感分类任务python源码+数据集.zip
- ####这是一篇对软件测试实验报告详细介绍(含代码)
- 这是一篇关于白盒测试的详细描述文章.docx
- ###这是一篇描述vector的教程文章
- 基于RISC-V的一个简单的五级流水线CPU设计源码+项目说明.zip
- 抽取地下水对加州约塞米蒂国家公园山地湿地复合体可持续性的影响
- C++vector介绍及详细使用示例(源代码)
- java基于GUI的简单记事本
- 指纹识别-基于OpenCV实现的指纹识别算法-附项目源码.zip
- 206693250008_R01C02_Grn.idat
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈