-
-
Save johlrich/80d99d67dac9d93fe3b5 to your computer and use it in GitHub Desktop.
akka.io echo service test via fsharp
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
open System | |
open System.Net | |
open Akka.Actor | |
open Akka.IO | |
open Akka.FSharp | |
let handle (message:obj) = | |
match message with | |
| :? Tcp.Bound as bound -> | |
printfn "Listening on %A" bound.LocalAddress | |
| :? Tcp.Connected as connected -> | |
printfn "New connection on %A" connected.LocalAddress | |
| _ -> () | |
// this will create a service whos Bound goes to deadletters and is not handled | |
// a connection can still be made to it though | |
let createEchoService1 (system:ActorSystem) = | |
let echoService = spawn system "echo-service1" (actorOf handle) | |
// rogeralsing: | |
// in the first case, you are not inside the context of an actor, and thus, there is no Sender given to the <! operator | |
// tell takes two arguments, message and sender. the last argument is optional and if you are inside an actor, Akka.NET will figure out who the current actor is and use that as sender | |
//system.Tcp() <! Tcp.Bind(echoService, IPEndPoint(IPAddress.Any, 8001)) | |
system.Tcp().Tell(Tcp.Bind(echoService, IPEndPoint(IPAddress.Any, 8001)), echoService) | |
echoService | |
// this seems fine | |
let createEchoService2 system = | |
spawn system "echo-service2" | |
<| fun mailbox -> | |
mailbox.Context.System.Tcp() <! Tcp.Bind(mailbox.Self, IPEndPoint(IPAddress.Any, 8002)) | |
let rec loop() = actor { | |
let! message = mailbox.Receive() | |
handle message | |
return! loop() | |
} | |
loop() | |
// this also seems fine | |
type EchoService() as self = | |
inherit ReceiveActor() | |
do | |
EchoService.Context.System.Tcp() <! Tcp.Bind(self.Self, IPEndPoint(IPAddress.Any, 8003)) | |
self.Receive<Tcp.Event>(handle) | |
let createEchoService3 (system:ActorSystem) = | |
system.ActorOf<EchoService>(name="echo-service3") | |
[<EntryPoint>] | |
let main argv = | |
use system = System.create "akkaio-test" <| Configuration.defaultConfig() | |
let host1 = createEchoService1 system | |
let host2 = createEchoService2 system | |
let host3 = createEchoService3 system | |
printfn "Press enter to exit" | |
Console.ReadLine() |> ignore | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment