/
ChatServer.fsx
200 lines (160 loc) · 6.12 KB
/
ChatServer.fsx
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#r "System.Xml.Linq.dll"
//#load "Server.fs"
//open System.IO
//open System.Net
//open System.Text
//open System.Threading
open System.Xml.Linq
//open FSharp.Control
type Agent<'T> = MailboxProcessor<'T>
// ----------------------------------------------------------------------------
module First =
let agent = Agent.Start(fun agent -> async {
while true do
let! msg = agent.Receive()
printfn "Hello %s!" msg })
agent.Post("Tomas")
type ChatMessage =
| GetContent of AsyncReplyChannel<string>
| SendMessage of string
module Second =
let agent = Agent<_>.Start(fun agent ->
let rec loop messages = async {
// Pick next message from the mailbox
let! msg = agent.Receive()
match msg with
| SendMessage msg ->
// Add message to the list & continue
let msg = XElement(XName.Get("li"), msg)
return! loop (msg :: messages)
| GetContent reply ->
// Generate HTML with messages
let html = XElement(XName.Get("ul"), messages)
// Send it back as the reply
reply.Reply(html.ToString())
return! loop messages }
loop [] )
agent.Post(SendMessage "Welcome to F# chat implemented using agents!")
agent.Post(SendMessage "This is my second message to this chat room...")
agent.PostAndReply(GetContent)
// --------------------------------------------------------------------------------------
type internal ChatMessage =
| GetContent of AsyncReplyChannel<string>
| SendMessage of string
type ChatRoom() =
let agent = Agent.Start(fun agent ->
let rec loop messages = async {
// Pick next message from the mailbox
let! msg = agent.Receive()
match msg with
| SendMessage msg ->
// Add message to the list & continue
let msg = XElement(XName.Get("li"), msg)
return! loop (msg :: messages)
| GetContent reply ->
// Generate HTML with messages
let html = XElement(XName.Get("ul"), messages)
// Send it back as the reply
reply.Reply(html.ToString())
return! loop messages }
loop [] )
member x.SendMessage(msg) = agent.Post(SendMessage msg)
member x.AsyncGetContent(?timeout) = agent.PostAndAsyncReply(GetContent, ?timeout=timeout)
member x.GetContent() = agent.PostAndReply(GetContent)
member x.GetContentAsync() =
Async.StartAsTask(agent.PostAndAsyncReply(GetContent))
member x.GetContentAsync(cancellationToken) =
Async.StartAsTask
( agent.PostAndAsyncReply(GetContent),
cancellationToken = cancellationToken )
let room = new ChatRoom()
room.SendMessage("Welcome to F# chat implemented using agents!")
room.SendMessage("This is my second message to this chat room...")
async {
while true do
do! Async.Sleep(10000)
let! html = room.AsyncGetContent()
printfn "%A" html }
|> Async.Start
// --------------------------------------------------------------------------------------
open System.Net
open System.Threading
[<AutoOpen>]
module HttpExtensions =
type System.Net.HttpListener with
member x.AsyncGetContext() =
Async.FromBeginEnd(x.BeginGetContext, x.EndGetContext)
type HttpAgent private (url, f) as this =
let tokenSource = new CancellationTokenSource()
let agent = Agent.Start((fun _ -> f this), tokenSource.Token)
let server = async {
use listener = new HttpListener()
listener.Prefixes.Add(url)
listener.Start()
while true do
let! context = listener.AsyncGetContext()
agent.Post(context) }
do Async.Start(server, cancellationToken = tokenSource.Token)
member x.Receive(?timeout) = agent.Receive(?timeout = timeout)
member x.Stop() = tokenSource.Cancel()
static member Start(url, f) =
new HttpAgent(url, f)
open System.IO
open System.Text
[<AutoOpen>]
module HttpExtensions2 =
type System.Net.HttpListenerRequest with
member request.InputString =
use sr = new StreamReader(request.InputStream)
sr.ReadToEnd()
type System.Net.HttpListenerResponse with
member response.Reply(s:string) =
let buffer = Encoding.UTF8.GetBytes(s)
response.ContentLength64 <- int64 buffer.Length
let output = response.OutputStream
output.Write(buffer,0,buffer.Length)
output.Close()
member response.Reply(typ, buffer:byte[]) =
response.ContentLength64 <- int64 buffer.Length
let output = response.OutputStream
response.ContentType <- typ
output.Write(buffer,0,buffer.Length)
output.Close()
// --------------------------------------------------------------------------------------
open System.Threading
let server = HttpAgent.Start("http://localhost:8082/", fun server -> async {
while true do
let! ctx = server.Receive()
ctx.Response.Reply("Hello!") })
server.Stop()
let root = @"C:\Tomas\Writing\MSDN\code\2 Server Side\Demo.ChatServer\"
let contentTypes =
dict [ ".gif", "binary/image"
".css", "text/css"
".html", "text/html"
".xap", "application/x-silverlight-app" ]
let server = HttpAgent.Start("http://localhost:8082/", fun mbox ->
let handleRequest (ctx:HttpListenerContext) = async {
match ctx.Request.Url.LocalPath with
| "/post" ->
// Send message to the chat room
room.SendMessage(ctx.Request.InputString)
ctx.Response.Reply("OK")
| "/chat" ->
// Get messages from the chat room (asynchronously!)
let! text = room.AsyncGetContent()
ctx.Response.Reply(text)
| s ->
// Handle an ordinary file request
let file =
root + (if s = "/" then "chat.html" else s.ToLower())
if File.Exists(file) then
let typ = contentTypes.[Path.GetExtension(file)]
ctx.Response.Reply(typ, File.ReadAllBytes(file))
else
ctx.Response.Reply(sprintf "File not found: %s" file) }
async {
while true do
let! ctx = mbox.Receive()
ctx |> handleRequest |> Async.Start })
server.Stop()