来源 http://cn.codeof.com/articles/.net/asp.net-development/562.htm
第二个我们要建立的是什么呢?一个聊天室应能存储在线成员的名字及访问时间
接下来我们就应该做这个asmx了
无刷新的聊天室的制作兼谈组件制作和ClientSide Script(一)
我们在传统的web程序当中比较头疼的一件事是屏幕的刷新感。虽然有server push的技术,但在IE中较难实现。现在webservice给了我们这样一个机会,大家都知道webservice是基于soap的,而soap是xml的应用,如果你曾经用过ms xml sdk3.0的话就会知道里面有个xmlhttp方法,其实在那时我们就已经可以用xmlhttp的方式替代Form了,也是无刷新的,其实准确地说是局部刷新,下面我们来看一下怎样做,先做一个chat webservice, 首先来分析一下,一个聊天室应具备的两个要素人和消息,这样我们可以建立一个类型(记得我在以前说过类也是类型),它包含这样两个要素。
1
//ChatMessage.cs
2
using System;
3
namespace chat
4
{
5
/// <summary>
6
/// ChatMessage类封装了两个string变量:UserLists--用户列表,Messages--要传递的信息
7
/// </summary>
8
public class ChatMessage
9
{
10
public string UserList, Messages;
11
}
12
}
13
//ChatMessage.cs2
using System;3
namespace chat4
{5
/// <summary>6
/// ChatMessage类封装了两个string变量:UserLists--用户列表,Messages--要传递的信息7
/// </summary>8
public class ChatMessage9
{10
public string UserList, Messages;11
}12
}13

第二个我们要建立的是什么呢?一个聊天室应能存储在线成员的名字及访问时间
1
///Member.cs
2
using System;
3
namespace chat
4
{
5
/// <summary>
6
/// Member类为每个聊天者封装了Server端的变量
7
/// </summary>
8
public class Member
9
{
10
// 存储消息的队列
11
public string UserName, MsgQueue;
12
// 判断滞留事件以便踢人
13
public System.DateTime LastAccessTime;
14
// The constructor
15
public Member(string NickName)
16
{
17
this.UserName=NickName;
18
this.LastAccessTime=DateTime.Now;
19
}
20
}
21
}
22
///Member.cs2
using System;3
namespace chat4
{5
/// <summary>6
/// Member类为每个聊天者封装了Server端的变量7
/// </summary>8
public class Member9
{10
// 存储消息的队列 11
public string UserName, MsgQueue;12
// 判断滞留事件以便踢人13
public System.DateTime LastAccessTime;14
// The constructor15
public Member(string NickName)16
{17
this.UserName=NickName;18
this.LastAccessTime=DateTime.Now;19
}20
}21
}22

1
///ChatWebService.asmx
2
using System;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Data;
6
using System.Diagnostics;
7
using System.Web;
8
using System.Web.Services;
9
namespace chat
10
{
11
/// <summary>
12
/// Summary description for ChatWebService.
13
/// </summary>
14
[WebService (Namespace = "http://localhost/chat/", Description = "This service provides an chat service")]
15
public class ChatWebService : System.Web.Services.WebService
16
{
17
public ChatWebService()
18
{
19
//CODEGEN: This call is required by the ASP.NET Web Services Designer
20
InitializeComponent();
21
}
22
#region Component Designer generated code
23
/// <summary>
24
/// Required method for Designer support - do not modify
25
/// the contents of this method with the code editor.
26
/// </summary>
27
28
private void InitializeComponent()
29
{
30
31
}
32
33
#endregion
34
/// <summary>
35
/// Clean up any resources being used.
36
/// </summary>
37
protected override void Dispose( bool disposing )
38
{
39
}
40
41
[WebMethod(Description="接收用户名作为参数存储到Application对象中")]
42
public string Login(string username)
43
{
44
// Ascertain that all the registered chat participants are active
45
CheckMembersList();
46
// Synchronization Lock
47
Application.Lock();
48
// Get the collection of keys for the Application Variables
49
String[] Members = Application.AllKeys;
50
// Are there any registered chat members? & the present request is for a unique nick name?
51
if ((Members.Length>0)&&(Array.IndexOf(Members,username)>-1))
52
{
53
throw new Exception("该用户已存在!");
54
}
55
56
// Create a new Member object for this participant
57
Member NewMember = new Member(username);
58
// Add this new member to the collectionof Application Level Variables
59
Application.Add(username, NewMember);
60
// Synchronization unlock
61
Application.UnLock();
62
// Go and get the list of current chat participants and retrun the list
63
return GetMembersList();
64
}
65
66
[WebMethod(Description="GetMsg方法用用户名和消息为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]
67
public ChatMessage XchangeMsgs(string username, string Msg)
68
{
69
// Ascertain that all the registered chat participants are active
70
CheckMembersList();
71
// Synchronization Lock
72
Application.Lock();
73
// Get the collection of keys for the Application Variables
74
String[] Members = Application.AllKeys;
75
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))
76
// Are there any registered chat members? & the present request is for a unique nick name?
77
{
78
throw new Exception("你当前可能没有登陆或登陆超时,请重新登陆!");
79
80
}
81
82
ChatMessage RetMsg = new ChatMessage();
83
84
85
86
RetMsg.UserList = GetMembersList();
87
88
// Loop through all the Chat Participant's serverside Member Objects and
89
90
// add the message just received in their waiting message queue
91
92
for (int x=0;x<Members.Length;x++)
93
94
{
95
96
Member temp = (Member)Application[Members[x]];
97
98
temp.MsgQueue+=("<BR><Font color = Red>" + username + " 说:<BR></FONT><Font color = Blue>" + Msg);
99
100
if (temp.UserName == username)
101
102
{
103
104
RetMsg.Messages = temp.MsgQueue;
105
106
temp.MsgQueue="";
107
108
temp.LastAccessTime=DateTime.Now;
109
110
}
111
112
}
113
114
// Synchronization unlock
115
116
Application.UnLock();
117
118
return RetMsg;
119
120
}
121
122
123
124
[WebMethod(Description="GetMsg方法用username为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]
125
126
public ChatMessage GetMsgs(string username)
127
128
{
129
130
Application.Lock();
131
132
CheckMembersList();
133
134
Application.Lock();
135
136
String[] Members = Application.AllKeys;
137
138
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))
139
140
{
141
142
throw new Exception("Unknown User. Please Login with a UserName");
143
144
}
145
146
ChatMessage RetMsg = new ChatMessage();
147
148
RetMsg.UserList = GetMembersList();
149
150
Member temp = (Member)Application[username];
151
152
RetMsg.Messages = temp.MsgQueue;
153
154
temp.MsgQueue="";
155
156
temp.LastAccessTime=DateTime.Now;
157
158
Application.UnLock();
159
160
return RetMsg;
161
162
}
163
164
165
166
public string GetMembersList()
167
168
{
169
170
Application.Lock();
171
172
String UserList = "";
173
174
String[] Members = Application.AllKeys;
175
176
Application.UnLock();
177
178
for (int x=0;x<Members.Length;x++)
179
180
{
181
182
Member temp = (Member)Application[Members[x]];
183
184
UserList += (temp.UserName+"\n");
185
186
}
187
188
return UserList;
189
190
}
191
192
193
194
private void CheckMembersList()
195
196
{
197
198
String[] Members = Application.AllKeys;
199
200
ArrayList RemoveList = new ArrayList();
201
202
for (int x=0;x<Members.Length;x++)
203
204
{
205
206
Member temp = (Member) Application[Members[x]];
207
208
int test = (DateTime.Now.Subtract(temp.LastAccessTime)).Minutes;
209
210
if (test > 2)
211
212
{
213
214
RemoveList.Add(Members[x]);
215
216
}
217
218
}
219
220
// Users = null;
221
222
for (int count = 0;count<RemoveList.Count;count++)
223
224
{
225
226
Application.Remove((String)RemoveList[count]);
227
228
}
229
230
return;
231
232
}
233
234
235
236
237
238
}
239
240
}
241
///ChatWebService.asmx2
using System;3
using System.Collections;4
using System.ComponentModel;5
using System.Data;6
using System.Diagnostics;7
using System.Web;8
using System.Web.Services;9
namespace chat10
{11
/// <summary>12
/// Summary description for ChatWebService.13
/// </summary>14
[WebService (Namespace = "http://localhost/chat/", Description = "This service provides an chat service")]15
public class ChatWebService : System.Web.Services.WebService16
{17
public ChatWebService()18
{19
//CODEGEN: This call is required by the ASP.NET Web Services Designer20
InitializeComponent();21
}22
#region Component Designer generated code23
/// <summary>24
/// Required method for Designer support - do not modify25
/// the contents of this method with the code editor.26
/// </summary>27

28
private void InitializeComponent()29
{30

31
}32

33
#endregion34
/// <summary>35
/// Clean up any resources being used.36
/// </summary>37
protected override void Dispose( bool disposing )38
{39
}40

41
[WebMethod(Description="接收用户名作为参数存储到Application对象中")]42
public string Login(string username)43
{44
// Ascertain that all the registered chat participants are active45
CheckMembersList();46
// Synchronization Lock47
Application.Lock();48
// Get the collection of keys for the Application Variables49
String[] Members = Application.AllKeys;50
// Are there any registered chat members? & the present request is for a unique nick name?51
if ((Members.Length>0)&&(Array.IndexOf(Members,username)>-1))52
{53
throw new Exception("该用户已存在!");54
}55

56
// Create a new Member object for this participant57
Member NewMember = new Member(username);58
// Add this new member to the collectionof Application Level Variables59
Application.Add(username, NewMember);60
// Synchronization unlock61
Application.UnLock();62
// Go and get the list of current chat participants and retrun the list63
return GetMembersList();64
}65

66
[WebMethod(Description="GetMsg方法用用户名和消息为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]67
public ChatMessage XchangeMsgs(string username, string Msg)68
{69
// Ascertain that all the registered chat participants are active70
CheckMembersList();71
// Synchronization Lock72
Application.Lock();73
// Get the collection of keys for the Application Variables74
String[] Members = Application.AllKeys;75
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))76
// Are there any registered chat members? & the present request is for a unique nick name?77
{78
throw new Exception("你当前可能没有登陆或登陆超时,请重新登陆!");79

80
}81

82
ChatMessage RetMsg = new ChatMessage();83

84

85

86
RetMsg.UserList = GetMembersList();87

88
// Loop through all the Chat Participant's serverside Member Objects and89

90
// add the message just received in their waiting message queue91

92
for (int x=0;x<Members.Length;x++)93

94
{95

96
Member temp = (Member)Application[Members[x]];97

98
temp.MsgQueue+=("<BR><Font color = Red>" + username + " 说:<BR></FONT><Font color = Blue>" + Msg);99

100
if (temp.UserName == username)101

102
{103

104
RetMsg.Messages = temp.MsgQueue;105

106
temp.MsgQueue="";107

108
temp.LastAccessTime=DateTime.Now;109

110
}111

112
}113

114
// Synchronization unlock115

116
Application.UnLock();117

118
return RetMsg;119

120
}121

122
123

124
[WebMethod(Description="GetMsg方法用username为参数返回一个ChatMessage对象,包括要传递的消息和用户列表")]125

126
public ChatMessage GetMsgs(string username)127

128
{129

130
Application.Lock();131

132
CheckMembersList();133

134
Application.Lock();135

136
String[] Members = Application.AllKeys;137

138
if ((Members.Length==0)||(Array.IndexOf(Members,username)==-1))139

140
{141

142
throw new Exception("Unknown User. Please Login with a UserName");143

144
}145

146
ChatMessage RetMsg = new ChatMessage();147

148
RetMsg.UserList = GetMembersList();149

150
Member temp = (Member)Application[username];151

152
RetMsg.Messages = temp.MsgQueue;153

154
temp.MsgQueue="";155

156
temp.LastAccessTime=DateTime.Now;157

158
Application.UnLock();159

160
return RetMsg;161

162
}163

164

165

166
public string GetMembersList()167

168
{169

170
Application.Lock();171

172
String UserList = "";173

174
String[] Members = Application.AllKeys;175

176
Application.UnLock();177

178
for (int x=0;x<Members.Length;x++)179

180
{181

182
Member temp = (Member)Application[Members[x]];183

184
UserList += (temp.UserName+"\n");185

186
}187

188
return UserList;189

190
}191

192

193

194
private void CheckMembersList()195

196
{197

198
String[] Members = Application.AllKeys;199

200
ArrayList RemoveList = new ArrayList();201

202
for (int x=0;x<Members.Length;x++) 203

204
{205

206
Member temp = (Member) Application[Members[x]];207

208
int test = (DateTime.Now.Subtract(temp.LastAccessTime)).Minutes;209

210
if (test > 2)211

212
{213

214
RemoveList.Add(Members[x]); 215

216
}217

218
}219

220
// Users = null;221

222
for (int count = 0;count<RemoveList.Count;count++)223

224
{225

226
Application.Remove((String)RemoveList[count]);227

228
}229

230
return;231

232
}233

234

235

236
237

238
}239

240
}241



浙公网安备 33010602011771号