-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from j-alexander/binary_websocket
add Binary(byte[]) WebSocket support
- Loading branch information
Showing
8 changed files
with
842 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
module Nata.IO.WebSocket.Tests.Host | ||
|
||
open System | ||
open System.Net | ||
open System.Text | ||
open System.Threading | ||
open Suave | ||
open Suave.Sockets | ||
open Suave.Sockets.Control | ||
open Suave.Successful | ||
open Suave.Operators | ||
open Suave.Writers | ||
open Suave.WebSocket | ||
open Suave.Filters | ||
|
||
let countText = | ||
handShake <| fun ws _ -> | ||
let rec loop(i) = | ||
socket { | ||
do! Async.Sleep(1000) | ||
|> Async.map Choice1Of2 | ||
let code = Opcode.Text | ||
let data = | ||
sprintf "%d" i | ||
|> Encoding.UTF8.GetBytes | ||
|> ByteSegment | ||
do! ws.send code data true | ||
return! loop(i+1) | ||
} | ||
socket { | ||
let! _ = ws.read() | ||
return! loop(0) | ||
} | ||
|
||
let countBinary = | ||
handShake <| fun ws _ -> | ||
let rec loop(i:int32) = | ||
socket { | ||
do! Async.Sleep(1000) | ||
|> Async.map Choice1Of2 | ||
let code = Opcode.Binary | ||
let data = | ||
BitConverter.GetBytes(i) | ||
|> ByteSegment | ||
do! ws.send code data true | ||
return! loop(i+1) | ||
} | ||
socket { | ||
let! _ = ws.read() | ||
return! loop(0) | ||
} | ||
|
||
let echo = | ||
handShake <| fun ws _ -> | ||
let rec loop() = | ||
socket { | ||
let! (code:Opcode, data:byte[], fin:bool) = ws.read() | ||
match code with | ||
| Opcode.Close -> | ||
return () | ||
| Opcode.Text | Opcode.Binary -> | ||
do! ws.send code (ByteSegment data) fin | ||
return! loop() | ||
| _ -> | ||
return! loop() | ||
} | ||
loop() | ||
|
||
let api : WebPart = | ||
choose | ||
[ GET >=> path "/count/text" >=> countText | ||
GET >=> path "/count/binary" >=> countBinary | ||
GET >=> pathStarts "/echo" >=> echo | ||
GET >=> OK "hello" ] | ||
>=> setMimeType "application/json; charset=utf-8" | ||
|
||
|
||
type Command = | ||
| Start | ||
| Stop | ||
|
||
type Service(port:uint16) = | ||
|
||
let service = MailboxProcessor.Start(fun (inbox:MailboxProcessor<Command*AsyncReplyChannel<unit>>) -> | ||
let rec waitForStart() = | ||
async { | ||
let! (message, sender) = inbox.Receive() | ||
match message with | ||
| Start -> | ||
let source = new CancellationTokenSource() | ||
let configuration = | ||
{ defaultConfig with | ||
bindings = [ HttpBinding.create HTTP IPAddress.Loopback port ] | ||
cancellationToken = source.Token } | ||
let started, service = startWebServerAsync configuration api | ||
Async.Start(service,source.Token) | ||
let! status = started | ||
sender.Reply() | ||
return! waitForStop(source) | ||
| Stop -> | ||
sender.Reply() | ||
return! waitForStart() | ||
} | ||
and waitForStop(source:CancellationTokenSource) = | ||
async { | ||
let! (message, sender) = inbox.Receive() | ||
match message with | ||
| Stop -> | ||
source.Cancel() | ||
source.Dispose() | ||
sender.Reply() | ||
return! waitForStart() | ||
| Start -> | ||
sender.Reply() | ||
return! waitForStop(source) | ||
} | ||
waitForStart()) | ||
do | ||
service.Error.Add(fun ex -> | ||
let message = ex.Message | ||
()) | ||
|
||
member x.Start() = service.PostAndReply(fun x -> Start, x) | ||
member x.Stop() = service.PostAndReply(fun x -> Stop, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ NUnit | |
FSharp.Core | ||
System.ValueTuple | ||
NUnit3TestAdapter | ||
Microsoft.NET.Test.Sdk | ||
Microsoft.NET.Test.Sdk | ||
Suave |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.