Skip to content

Commit eec9960

Browse files
author
Jimmy Byrd
committed
TPL Doc comments
1 parent aabeeb8 commit eec9960

File tree

6 files changed

+391
-728
lines changed

6 files changed

+391
-728
lines changed

src/FSharp.Control.Websockets.TPL/FSharp.Control.Websockets.TPL.fs

Lines changed: 356 additions & 52 deletions
Large diffs are not rendered by default.

src/FSharp.Control.Websockets/FSharp.Control.Websockets.fs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ module Stream =
7979
|> Text.Encoding.UTF8.GetString
8080
|> fun s -> s.TrimEnd(char 0) // remove null teriminating characters
8181

82-
8382
/// **Description**
8483
///
8584
/// Turns a `MemoryStream` into a a UTF8 string
@@ -111,6 +110,20 @@ module WebSocket =
111110
[<Literal>]
112111
let DefaultBufferSize : int = 16384 // (16 * 1024)
113112

113+
/// **Description**
114+
///
115+
/// Determines if the websocket is open
116+
///
117+
/// **Parameters**
118+
/// * `socket` - parameter of type `WebSocket`
119+
///
120+
/// **Output Type**
121+
/// * `bool`
122+
///
123+
/// **Exceptions**
124+
///
125+
let isWebsocketOpen (socket : WebSocket) =
126+
socket.State = WebSocketState.Open
114127

115128
/// **Description**
116129
///
@@ -188,21 +201,6 @@ module WebSocket =
188201
|> Async.AwaitTaskWithCancellation
189202

190203

191-
/// **Description**
192-
///
193-
/// Determines if the websocket is open
194-
///
195-
/// **Parameters**
196-
/// * `socket` - parameter of type `WebSocket`
197-
///
198-
/// **Output Type**
199-
/// * `bool`
200-
///
201-
/// **Exceptions**
202-
///
203-
let isWebsocketOpen (socket : WebSocket) =
204-
socket.State = WebSocketState.Open
205-
206204
/// **Description**
207205
///
208206
/// Sends a whole message to the websocket read from the given stream
@@ -218,7 +216,7 @@ module WebSocket =
218216
///
219217
/// **Exceptions**
220218
///
221-
let sendMessage (socket : WebSocket) (bufferSize : int) messageType (readableStream : #IO.Stream) = async {
219+
let sendMessage (socket : WebSocket) (bufferSize : int) (messageType : WebSocketMessageType) (readableStream : #IO.Stream) = async {
222220
let buffer = Array.create (bufferSize) Byte.MinValue
223221

224222
let rec sendMessage' () = async {
@@ -247,7 +245,7 @@ module WebSocket =
247245
///
248246
/// **Exceptions**
249247
///
250-
let sendMessageAsUTF8 (socket : WebSocket) text = async {
248+
let sendMessageAsUTF8 (socket : WebSocket) (text : string) = async {
251249
use stream = IO.MemoryStream.UTF8toMemoryStream text
252250
do! sendMessage socket DefaultBufferSize WebSocketMessageType.Text stream
253251
}
@@ -268,7 +266,7 @@ module WebSocket =
268266
/// **Parameters**
269267
/// * `socket` - parameter of type `WebSocket`
270268
/// * `bufferSize` - parameter of type `int` - How many bytes to read from the socket at a time. Recommended to use `DefaultBufferSize`.
271-
/// * `messageType` - parameter of type `WebSocketMessageType` - Indicates whether the application is sending a binary or text message.
269+
/// * `messageType` - parameter of type `WebSocketMessageType` - Indicates whether the application is receiving a binary or text message.
272270
/// * `writeableStream` - parameter of type `IO.Stream` - A writeable stream that data from the websocket is written into.
273271
///
274272
/// **Output Type**
@@ -304,7 +302,7 @@ module WebSocket =
304302
/// Reading from the websocket completed.
305303
| String of string
306304
/// The websocket was closed during reading.
307-
| StreamClosed of closeStatus: WebSocketCloseStatus * closeStatusDescription:string
305+
| Closed of closeStatus: WebSocketCloseStatus * closeStatusDescription:string
308306

309307

310308
/// **Description**
@@ -325,8 +323,8 @@ module WebSocket =
325323
match result with
326324
| ReceiveStreamResult.Stream s ->
327325
return stream |> IO.MemoryStream.ToUTF8String |> String
328-
| Closed(status, reason) ->
329-
return ReceiveUTF8Result.StreamClosed(status, reason)
326+
| ReceiveStreamResult.Closed(status, reason) ->
327+
return ReceiveUTF8Result.Closed(status, reason)
330328
}
331329

332330
module ThreadSafeWebSocket =
@@ -342,19 +340,25 @@ module ThreadSafeWebSocket =
342340

343341
type ReceiveMessage = int * WebSocketMessageType * IO.Stream * AsyncReplyChannel<Result<WebSocket.ReceiveStreamResult, ExceptionDispatchInfo>>
344342

345-
/// The ThreadSafeWebSocket record allows applications to send and receive data after the WebSocket upgrade has completed. This puts a `MailboxProcessor` in front of all send and receive messages to prevent multiple threads reading or writing to the socket at a time. Without this a websocket send/receive may throw a `InvalidOperationException` with the message: `There is already one outstanding 'SendAsync' call for this WebSocket instance. ReceiveAsync and SendAsync can be called simultaneously, but at most one outstanding operation for each of them is allowed at the same time.`
343+
/// The ThreadSafeWebSocket record allows applications to send and receive data after the WebSocket upgrade has completed. This puts a `MailboxProcessor` in front of all send and receive messages to prevent multiple threads reading or writing to the socket at a time. Without this a websocket send/receive may throw a `InvalidOperationException` with the message:
344+
///
345+
/// `There is already one outstanding 'SendAsync' call for this WebSocket instance. ReceiveAsync and SendAsync can be called simultaneously, but at most one outstanding operation for each of them is allowed at the same time.`
346346
type ThreadSafeWebSocket =
347347
{ websocket : WebSocket
348348
sendChannel : MailboxProcessor<SendMessages>
349349
receiveChannel : MailboxProcessor<ReceiveMessage>
350350
}
351351
interface IDisposable with
352+
/// Used to clean up unmanaged resources for ASP.NET and self-hosted implementations.
352353
member x.Dispose() =
353354
x.websocket.Dispose()
355+
/// Returns the current state of the WebSocket connection.
354356
member x.State =
355357
x.websocket.State
358+
/// Indicates the reason why the remote endpoint initiated the close handshake.
356359
member x.CloseStatus =
357360
x.websocket.CloseStatus |> Option.ofNullable
361+
///Allows the remote endpoint to describe the reason why the connection was closed.
358362
member x.CloseStatusDescription =
359363
x.websocket.CloseStatusDescription
360364

@@ -456,7 +460,7 @@ module ThreadSafeWebSocket =
456460

457461
/// **Description**
458462
///
459-
/// Reads an entire message from a websocket.
463+
/// Reads an entire message from a websocket as a string.
460464
///
461465
/// **Parameters**
462466
/// * `threadSafeWebSocket` - parameter of type `ThreadSafeWebSocket`
@@ -491,7 +495,7 @@ module ThreadSafeWebSocket =
491495
match response with
492496
| Ok (WebSocket.ReceiveStreamResult.Stream s) -> return stream |> IO.MemoryStream.ToUTF8String |> WebSocket.ReceiveUTF8Result.String |> Ok
493497
| Ok (WebSocket.Closed(status, reason)) ->
494-
return Ok (WebSocket.ReceiveUTF8Result.StreamClosed(status, reason))
498+
return Ok (WebSocket.ReceiveUTF8Result.Closed(status, reason))
495499
| Error ex -> return Error ex
496500

497501
}

tests/FSharp.Control.Websockets.TPL.Tests/Tests.fs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ let echoWebSocket (httpContext : HttpContext) (next : unit -> Task) = task {
2828
let! (websocket : WebSocket) = httpContext.WebSockets.AcceptWebSocketAsync() |> Async.AwaitTask
2929
while websocket.State = WebSocketState.Open do
3030
try
31-
let! result =
32-
websocket
33-
|> WebSocket.receiveMessageAsUTF8 CancellationToken.None
31+
let! result =WebSocket.receiveMessageAsUTF8 websocket CancellationToken.None
3432
match result with
3533
| WebSocket.ReceiveUTF8Result.String text ->
36-
do! WebSocket.sendMessageAsUTF8 CancellationToken.None text websocket
37-
| WebSocket.ReceiveUTF8Result.StreamClosed (status, reason) ->
34+
do! WebSocket.sendMessageAsUTF8 websocket CancellationToken.None text
35+
| WebSocket.ReceiveUTF8Result.Closed (status, reason) ->
3836
printfn "Socket closed %A - %s" status reason
3937
with e ->
4038
printfn "%A" e
@@ -67,7 +65,7 @@ let tests =
6765
Expect.equal actual (Ok <| WebSocket.ReceiveUTF8Result.String expected) "did not echo"
6866

6967

70-
let! _ = ThreadSafeWebSocket.close clientWebSocket CancellationToken.None WebSocketCloseStatus.NormalClosure "Closing"
68+
let! _ = ThreadSafeWebSocket.close clientWebSocket WebSocketCloseStatus.NormalClosure "Closing" CancellationToken.None
7169
Expect.equal clientWebSocket.State WebSocketState.Closed "Should be closed"
7270
}
7371

@@ -82,7 +80,7 @@ let tests =
8280
Expect.equal actual (Ok <| WebSocket.ReceiveUTF8Result.String expected) "did not echo"
8381

8482

85-
let! _ = ThreadSafeWebSocket.closeOutput clientWebSocket CancellationToken.None WebSocketCloseStatus.NormalClosure "Closing"
83+
let! _ = ThreadSafeWebSocket.closeOutput clientWebSocket WebSocketCloseStatus.NormalClosure "Closing" CancellationToken.None
8684
Expect.equal clientWebSocket.State WebSocketState.CloseSent "Should have sent closed without waiting acknowledgement"
8785
}
8886
]

0 commit comments

Comments
 (0)