Typed SignalR hubs for the Moon-Lander framework: handlers, addressing, groups and hosting
client --sendMessage--> server session --> handler --> Target::OthersInGroup(room)
|
groups actor: members of the room
|
other client <--receiveMessage-- outgoing queue of each member| Package | Targets | Contents |
|---|---|---|
| moonrockz/moon-lander-hubs | wasm, wasm-gc, js, native | Handler binding, Target, HubContext, Membership, Recorder |
| moonrockz/moon-lander-hubs/runtime | native, wasm | HubRuntime: the groups actor and the link to a server |
| moonrockz/moon-lander-hubs/hosting | native, wasm | hub_service: a hub server in a host |
///|
let post : @signalr.HubMethod[(String, String), Unit] = @signalr.HubMethod::call(
@signalr.MethodName::parse("post") catch {
_ => panic()
},
@signalr.args2(@signalr.string(), @signalr.string()),
@signalr.unit(),
)
///|
let enter : @signalr.HubMethod[String, Unit] = @signalr.HubMethod::call(
@signalr.MethodName::parse("enter") catch {
_ => panic()
},
@signalr.args1(@signalr.string()),
@signalr.unit(),
)
///|
let posted : @signalr.ClientMethod[(String, String)] = @signalr.ClientMethod::new(
@signalr.MethodName::parse("posted") catch {
_ => panic()
},
@signalr.args2(@signalr.string(), @signalr.string()),
)
///|
fn chat_room_hub() -> @hubs.Hub raise {
let contract = @signalr.HubContract::new(@signalr.HubName::parse("chat"))
.server(post)
.server(enter)
.client(posted)
@hubs.bind(contract)
.on(enter, (context, room) => {
context
.groups()
.add(context.connection_id(), @signalr.GroupName::parse(room))
})
.on(
post,
(context, arguments) => {
let (room, text) = arguments
guard context.user_id() is Some(user) else {
raise @signalr.HubError("sign in first")
}
context
.clients()
.others_in_group(@signalr.GroupName::parse(room))
.send(posted, (user.to_string(), text))
},
policy=identity => identity.user_id() is Some(_),
)
.build()
}
///|
test "each hub method has a handler" {
assert_eq(chat_room_hub().contract().hub_methods().length(), 2)
}///|
async test "a post goes to the others in the room" {
let recorder = @hubs.Recorder::new()
let context = recorder.context(
@signalr.ConnectionId::parse("A"),
identity=@signalr.Identity::user(@signalr.UserId::parse("ana")),
)
chat_room_hub().invoke(context, call_of("post", ["room1", "hi"])) |> ignore
assert_eq(recorder.sent()[0].target, OthersInGroup(room1, a))
}| Call | Target |
|---|---|
| clients().all() | Each connection |
| clients().caller() | The connection of the current call; it also has invoke for a typed result |
| clients().others() | Each connection except the caller |
| clients().client(id), .any().clients(ids) | These connections |
| clients().any().all_except(ids) | Each connection except these |
| clients().group(g), .any().groups(gs) | The connections of one or more groups |
| clients().any().group_except(g, ids), .others_in_group(g) | A group except some connections, or except the caller |
| clients().user(u), .any().users(us) | Each connection of one or more users |
pub suberror BindError {
MissingHandlers(Array[MethodName])
NotInContract(MethodName)
DuplicateHandler(MethodName)
} derive(Eq, Debug)pub(all) suberror ClientCallError {
Disconnected(ConnectionId)
ClientFailed(String)
} derive(Eq, Debug)pub struct Backend {
// private fields
}fn Backend::new(send~ : async (Target, MethodName, Array[HubValue]) -> Unit, invoke~ : async (ConnectionId, MethodName, Array[HubValue]) -> Outcome, join~ : async (ConnectionId, GroupName) -> Unit, leave~ : async (ConnectionId, GroupName) -> Unit, read_upload~ : async (StreamId) -> HubValue?) -> Backendpub struct CallerClients {
// private fields
}pub struct ClientProxy {
// private fields
}pub struct Clients {
// private fields
}fn Clients::group_except(self : Clients, group : GroupName, ids : ArrayView[ConnectionId]) -> ClientProxypub(all) enum GroupChange {
Added(ConnectionId, GroupName)
Removed(ConnectionId, GroupName)
} derive(Eq, Debug)pub struct GroupManager {
// private fields
}pub struct Hub {
// private fields
}pub struct HubBuilder {
// private fields
}fn[A, R] HubBuilder::on(self : HubBuilder, method_ : HubMethod[A, R], handler : async (HubContext, A) -> R, policy? : (Identity) -> Bool) -> HubBuilder raise BindErrorfn HubBuilder::on_disconnected(self : HubBuilder, handler : async (HubContext, DisconnectCause) -> Unit) -> HubBuilderfn[A, R] HubBuilder::on_stream(self : HubBuilder, method_ : HubMethod[A, R], handler : async (HubContext, A, async (R) -> Unit) -> Unit, policy? : (Identity) -> Bool) -> HubBuilder raise BindErrorpub struct HubContext {
// private fields
}fn HubContext::new(connection_id : ConnectionId, identity : Identity, backend : Backend, uploads? : ReadOnlyArray[StreamId]) -> HubContextpub struct Membership {
// private fields
}pub struct Recorder {
// private fields
}fn Recorder::context(self : Recorder, connection_id : ConnectionId, identity? : Identity) -> HubContextpub struct SingleClient {
// private fields
}async fn[A, R] SingleClient::invoke(self : SingleClient, call : ClientCall[A, R], arguments : A) -> Rasync fn[A] SingleClient::send(self : SingleClient, method_ : ClientMethod[A], arguments : A) -> Unitpub(all) enum Target {
All
Caller(ConnectionId)
Others(ConnectionId)
Connections(ReadOnlyArray[ConnectionId])
AllExcept(ReadOnlyArray[ConnectionId])
Groups(ReadOnlyArray[GroupName])
GroupExcept(GroupName, ReadOnlyArray[ConnectionId])
OthersInGroup(GroupName, ConnectionId)
Users(ReadOnlyArray[UserId])
} derive(Eq, Debug)Install
Download zipTyped SignalR hubs for the Moon-Lander framework: handlers, addressing, groups and hosting