forked from OrleansContrib/Orleankka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatServer.fs
39 lines (29 loc) · 1.37 KB
/
ChatServer.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module ChatServer
open System.Linq
open System.Collections.Generic
open Orleankka
open Orleankka.FSharp
open Messages
type ChatServer() =
inherit Actor<ServerMessage>()
let _users = Dictionary<string, IObserverCollection>()
let notifyClients msg = _users.Values |> Seq.iter(fun clients -> clients.Notify(msg))
override this.Receive message = task {
match message with
| Join (userName, client) ->
match _users.TryGetValue(userName) with
| (true, clients) -> clients.Add(client)
| _ -> Notification(sprintf "User: %s connected..." userName) |> notifyClients
let clients = ObserverCollection() :> IObserverCollection
clients.Add(client)
_users.Add(userName, clients)
return response("Hello and welcome to Orleankka chat example")
| Say (userName, text) -> NewMessage(userName, text) |> notifyClients
return response()
| Disconnect (userName, client) ->
match _users.TryGetValue(userName) with
| (true, clients) -> if clients.Count() = 1 then _users.Remove(userName) |> ignore
else clients.Remove(client)
return response()
| _ -> return response()
}