This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Samples – A chat room
haf edited this page Sep 23, 2011
·
1 revision
namespace ChatRoomDemo
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ChatRoom.Messages;
using Magnum.Extensions;
using Stact;
public class Program
{
public static void Main()
{
Console.WriteLine("Starting Chat Room Client");
var server = AnonymousActor.New(inbox =>
{
IDictionary<string, UntypedChannel> users = new Dictionary<string, UntypedChannel>();
inbox.Loop(loop =>
{
loop.Receive<ChatMessage>(m =>
{
users.Values.Each(x => x.Send(m));
loop.Continue();
});
loop.Receive<Request<AddUser>>(request =>
{
users.Add(request.Body.User, request.ResponseChannel);
Console.WriteLine("User added: " + request.Body.User);
request.Respond(new UserAdded { User = request.Body.User });
loop.Continue();
});
});
});
while (ReadLoop(server))
{
}
}
static bool ReadLoop(ActorInstance server)
{
string value = Console.ReadLine();
if (string.Compare(value, "exit", StringComparison.InvariantCultureIgnoreCase) == 0)
return false;
if (string.Compare(value, "quit", StringComparison.InvariantCultureIgnoreCase) == 0)
return false;
string[] parts = value.Split(':');
if (parts.Length == 2)
{
var message = new ChatMessage { Text = parts[1], User = parts[0] };
server.Send(message);
}
parts = value.Split('+');
if (parts.Length == 2)
{
var userName = parts[1];
var message = new AddUser { User = userName };
var user = AnonymousActor.New(inbox =>
{
inbox.Loop(loop =>
{
loop.Receive<ChatMessage>(m =>
{
if (userName != m.User)
Console.WriteLine("{0}: Received {1} from {2}", userName, m.Text, m.User);
loop.Continue();
});
loop.Receive<Response<UserAdded>>(response =>
{
Console.WriteLine("Was added: " + response.Body.User);
loop.Continue();
});
});
});
server.Request(message, user);
}
return true;
}
void AddSomething()
{
var factory = ActorFactory.Create(x => new ChatActor(x));
var actorInstance = factory.GetActor();
actorInstance.Send()
}
}
public class UserAdded
{
public string User { get; set; }
}
}