/
EchoServer.cs
40 lines (36 loc) · 1.26 KB
/
EchoServer.cs
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
//-----------------------------------------------------------------------
// <copyright file="EchoServer.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Net;
using Akka.Actor;
using Akka.IO;
namespace DocsExamples.Networking.IO
{
// <echoServer>
public class EchoServer : UntypedActor
{
public EchoServer(int port)
{
Context.System.Tcp().Tell(new Tcp.Bind(Self, new IPEndPoint(IPAddress.Any, port)));
}
protected override void OnReceive(object message)
{
if (message is Tcp.Bound)
{
var bound = message as Tcp.Bound;
Console.WriteLine("Listening on {0}", bound.LocalAddress);
}
else if (message is Tcp.Connected)
{
var connection = Context.ActorOf(Props.Create(() => new EchoConnection(Sender)));
Sender.Tell(new Tcp.Register(connection));
}
else Unhandled(message);
}
}
// </echoServer>
}