NetServer.cs
7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using SuperWebSocket;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
namespace NetLib
{
public class NetServer
{
WebSocketServer SererObj = null;
Dictionary<string, WebSocketSession> DicSession = new Dictionary<string, WebSocketSession>();
public delegate void DelSessionConnected(NetSession session);
public event DelSessionConnected SessionConnected;
public delegate void DelSessionClosed(NetSession session, CloseReason reason);
public event DelSessionClosed SessionClosed;
public delegate void DelMessageReceived(NetSession session, string value);
public event DelMessageReceived MessageReceived;
public delegate void DelDataReceived(NetSession session, byte[] value);
public event DelDataReceived DataReceived;
public delegate void DelSessionErr(NetSession session, Exception ex);
public event DelSessionErr SessionErr;
public bool Setup(int port, string ip = "")
{
bool res = false;
if (SererObj != null)
{
try
{
SererObj.Stop();
}
catch (Exception ex)
{
string sErr = ex + "";
}
SererObj.NewSessionConnected -= WebSvr_SessionConnected;
SererObj.SessionClosed -= WebSvr_SessionClosed;
SererObj.NewMessageReceived -= WebSvr_MessageReceived;
SererObj.NewDataReceived -= WebSvr_DataReceived;
SererObj.Dispose();
}
SererObj = new WebSocketServer();
if (string.IsNullOrEmpty(ip))
res = SererObj.Setup(port);
else
res = SererObj.Setup(ip, port);
if (res)
{
SererObj.NewSessionConnected += WebSvr_SessionConnected;
SererObj.SessionClosed += WebSvr_SessionClosed;
SererObj.NewMessageReceived += WebSvr_MessageReceived;
SererObj.NewDataReceived += WebSvr_DataReceived;
}
return res;
}
private NetSession GetSession(WebSocketSession session)
{
NetSession res = new NetSession();
res.ID = session.SessionID;
res.DicPar = session.Cookies;
if (res.DicPar == null)
res.DicPar = new StringDictionary();
res.LocalEndPoint = session.LocalEndPoint;
res.RemoteEndPoint = session.RemoteEndPoint;
return res;
}
private void WebSvr_SessionConnected(WebSocketSession session)
{
NetSession se = GetSession(session);
SessionConnected?.Invoke(se);
//if (session.Cookies["Pwd"] != "pwd111")
//{
// session.Send("拒绝访问");
// session.Close();
//}
}
private void WebSvr_SessionClosed(WebSocketSession session, SuperSocket.SocketBase.CloseReason value)
{
NetSession se = GetSession(session);
CloseReason value2 = (CloseReason)value;
SessionClosed?.Invoke(se, value2);
}
private void WebSvr_MessageReceived(WebSocketSession session, string value)
{
NetSession se = GetSession(session);
MessageReceived?.Invoke(se, value);
}
private void WebSvr_DataReceived(WebSocketSession session, byte[] value)
{
NetSession se = GetSession(session);
DataReceived?.Invoke(se, value);
}
public bool IsStart
{
get
{
return ((SererObj != null) && (SererObj.State == SuperSocket.SocketBase.ServerState.Running));
}
}
public bool Start()
{
if (SererObj == null)
return false;
else
return SererObj.Start();
}
public void Stop()
{
if (SererObj != null)
SererObj.Stop();
}
public void CloseSession(string ID)
{
if (SererObj == null)
return;
WebSocketSession session = SererObj.GetSessionByID(ID);
try
{
session.Close();
}
catch (Exception ex)
{
string sErr = ex + "";
}
}
/// <summary>
/// 杨斌 2020-05-16
/// </summary>
void Send(WebSocketSession se, string msg)
{
try
{
se.Send(msg);
}
catch (Exception ex)
{
string sErr = "" + ex;
NetSession session = GetSession(se);
SessionErr?.Invoke(session, ex);
}
}
/// <summary>
/// 杨斌 2020-05-16
/// </summary>
void Send(WebSocketSession se, byte[] data)
{
try
{
se.Send(data, 0, data.Length);
}
catch (Exception ex)
{
string sErr = "" + ex;
NetSession session = GetSession(se);
SessionErr?.Invoke(session, ex);
}
}
public void Send(string msg)
{
if (SererObj == null)
return;
foreach (var se in SererObj.GetAllSessions())
{
Send(se, msg);//se.Send(msg);杨斌 2020-05-16
}
}
public void Send(string msg, IEnumerable<string> IDs)
{
if (SererObj == null)
return;
foreach (var v in IDs)
{
WebSocketSession se = SererObj.GetSessionByID(v);
Send(se, msg); //se.Send(msg);杨斌 2020-05-16
}
}
public void Send(string msg, params string[] IDs)
{
if (SererObj == null)
return;
foreach (var v in IDs)
{
WebSocketSession se = SererObj.GetSessionByID(v);
Send(se, msg);//se.Send(msg);杨斌 2020-05-16
}
}
public void Send(byte[] data)
{
if (SererObj == null)
return;
foreach (var se in SererObj.GetAllSessions())
{
Send(se, data);// se.Send(data, 0, data.Length);杨斌 2020-05-16
}
}
public void Send(byte[] data, IEnumerable<string> IDs)
{
if (SererObj == null)
return;
foreach (var v in IDs)
{
WebSocketSession se = SererObj.GetSessionByID(v);
Send(se, data);//se.Send(data, 0, data.Length);杨斌 2020-05-16
}
}
public void Send(byte[] data, params string[] IDs)
{
if (SererObj == null)
return;
foreach (var v in IDs)
{
WebSocketSession se = SererObj.GetSessionByID(v);
Send(se, data);//se.Send(data, 0, data.Length);杨斌 2020-05-16
}
}
}
public class NetSession
{
public string ID = "";
public string No = "";//杨斌 2020-05-16
public IPEndPoint RemoteEndPoint = null;
public IPEndPoint LocalEndPoint = null;
public StringDictionary DicPar = null;
}
public enum CloseReason
{
Unknown = 0,
ServerShutdown = 1,
ClientClosing = 2,
ServerClosing = 3,
ApplicationError = 4,
SocketError = 5,
TimeOut = 6,
ProtocolError = 7,
InternalError = 8
}
}