📄 presence.cs
字号:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Web;
using Devspace.Commons.Tracer;
using System.IO;
using System.Collections.Generic;
using System.Collections;
namespace Devspace.Ajax.PersistentCommunications {
public class GlobalPresence : AuthenticatedHttpHandlerBase {
protected internal string constUserIdentifier = "user-identifier";
static VersionTracker _version = new VersionTracker( 10000);
static List<string> _users = new List<string>();
private const string constXStateHeader = "X-Last-State";
private const string constUseAction = "user-action";
public GlobalPresence() {
AssignUserResolver( new UserResolverImplementation());
}
protected internal override void DoGet(HttpRequest req, HttpResponse resp) {
string lastState = req.Headers.Get(constXStateHeader);
if (lastState == null) {
lastState = req.Params.Get(constXStateHeader);
}
GenerateOutput.Write("GlobalPresence.DoGet", "lastState (" + lastState + ")");
int lastCount = 0;
if (lastState != null) {
lastCount = int.Parse(lastState);
}
if (_version.GetNew(lastCount)) {
lock (_version) {
TextWriter output = resp.Output;
GenerateOutput.Write("GlobalPresence.DoGet", "Before writing users");
foreach( string user in _users) {
output.WriteLine( "<b>" + user + "</b><br />");
GenerateOutput.Write("GlobalPresence.DoGet", "Writing user (" + user + ")");
}
GenerateOutput.Write("GlobalPresence.DoGet", "After writing users");
}
resp.AddHeader(constXStateHeader, _version.VersionNumber.ToString());
GenerateOutput.Write("GlobalPresence.DoGet", "Writing something changed");
}
else {
resp.StatusDescription = "No change";
resp.StatusCode = 408;
GenerateOutput.Write("GlobalPresence.DoGet", "Writing nothing changed");
}
}
protected internal override void DoPost(HttpRequest req, HttpResponse resp) {
lock( _version) {
string action = GetVariable( req, constUseAction);
if( action == "add") {
GenerateOutput.Write("GlobalPresence.DoPost", "Added a user (" + GetVariable( req, constUserIdentifier) + ")");
_users.Add( GetVariable( req, constUserIdentifier));
}
else if( action == "remove") {
GenerateOutput.Write("GlobalPresence.DoPost", "Removed a user (" + GetVariable( req, constUserIdentifier) + ")");
_users.Remove( GetVariable( req, constUserIdentifier));
}
else {
GenerateOutput.Write("GlobalPresence.DoPost", "Did nothing, not right!");
}
}
_version.Increment();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -