websocket-sharp/Example2/Chat.cs

40 lines
779 B
C#
Raw Normal View History

2012-08-06 13:34:39 +08:00
using System;
2012-10-26 13:58:50 +08:00
using System.Threading;
2012-08-06 13:34:39 +08:00
using WebSocketSharp;
using WebSocketSharp.Server;
namespace Example2
{
2012-08-06 13:34:39 +08:00
public class Chat : WebSocketService
{
2012-10-26 13:58:50 +08:00
private static int _num = 0;
private string _name;
private string getName ()
{
return Context.QueryString ["name"] ?? ("anon#" + getNum ());
}
private int getNum ()
{
return Interlocked.Increment (ref _num);
}
protected override void OnOpen ()
{
_name = getName ();
}
protected override void OnMessage (MessageEventArgs e)
{
Sessions.Broadcast (String.Format ("{0}: {1}", _name, e.Data));
}
protected override void OnClose (CloseEventArgs e)
2012-08-06 13:34:39 +08:00
{
Sessions.Broadcast (String.Format ("{0} got logged off...", _name));
2012-08-06 13:34:39 +08:00
}
}
}