websocket-sharp/Example2/Chat.cs

45 lines
844 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()
{
2013-05-08 23:41:02 +08:00
return QueryString.Contains("name")
? QueryString["name"]
: "anon#" + getNum();
}
2012-10-26 13:58:50 +08:00
private int getNum()
{
2012-10-26 13:58:50 +08:00
return Interlocked.Increment(ref _num);
}
protected override void OnOpen()
{
_name = getName();
}
protected override void OnMessage(MessageEventArgs e)
{
var msg = String.Format("{0}: {1}", _name, e.Data);
Broadcast(msg);
}
protected override void OnClose(CloseEventArgs e)
2012-08-06 13:34:39 +08:00
{
var msg = String.Format("{0} got logged off...", _name);
Broadcast(msg);
2012-08-06 13:34:39 +08:00
}
}
}