You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
880 B
C#
43 lines
880 B
C#
using System;
|
|
using WebSocketSharp;
|
|
using WebSocketSharp.Server;
|
|
|
|
namespace Example3
|
|
{
|
|
public class Chat : WebSocketService
|
|
{
|
|
private static object _forId = new object();
|
|
private static uint _id = 0;
|
|
|
|
private string _name;
|
|
|
|
private string getName()
|
|
{
|
|
lock (_forId)
|
|
{
|
|
return QueryString.Exists("name")
|
|
? QueryString["name"]
|
|
: "anon#" + (++_id);
|
|
}
|
|
}
|
|
|
|
protected override void OnOpen(object sender, EventArgs e)
|
|
{
|
|
_name = getName();
|
|
}
|
|
|
|
protected override void OnMessage(object sender, MessageEventArgs e)
|
|
{
|
|
|
|
var msg = String.Format("{0}: {1}", _name, e.Data);
|
|
Publish(msg);
|
|
}
|
|
|
|
protected override void OnClose(object sender, CloseEventArgs e)
|
|
{
|
|
var msg = String.Format("{0} got logged off...", _name);
|
|
Publish(msg);
|
|
}
|
|
}
|
|
}
|