Skip to content

Commit 88d6981

Browse files
author
Eldin Zenderink
committed
v1.0.2
-Fixed bug where server thinks connection is open when unexpectedly closing the websocket connection (by closing or refreshing a webpage with a websocket client on chrome for example).
1 parent 79e2a6d commit 88d6981

11 files changed

Lines changed: 384 additions & 225 deletions

File tree

SimpleWebsocketServer.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.27428.2005
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleWebsocketServer", "SimpleWebsocketServer\SimpleWebsocketServer.csproj", "{AF7A3511-2D27-4BED-B81F-304C508D016B}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleWebsocketServer", "SimpleWebsocketServer\SimpleWebsocketServer.csproj", "{AF7A3511-2D27-4BED-B81F-304C508D016B}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebsocketServerTest", "WebsocketServerTest\WebsocketServerTest.csproj", "{D964861E-249F-42CA-802C-7D02A7872DBC}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSocketServerTestNET", "WebSocketServerTestNET\WebSocketServerTestNET.csproj", "{8CE88C69-3E35-4617-A2BD-B4F66244FCA0}"
99
ProjectSection(ProjectDependencies) = postProject
1010
{AF7A3511-2D27-4BED-B81F-304C508D016B} = {AF7A3511-2D27-4BED-B81F-304C508D016B}
1111
EndProjectSection
@@ -20,10 +20,10 @@ Global
2020
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Debug|Any CPU.Build.0 = Debug|Any CPU
2121
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Release|Any CPU.ActiveCfg = Release|Any CPU
2222
{AF7A3511-2D27-4BED-B81F-304C508D016B}.Release|Any CPU.Build.0 = Release|Any CPU
23-
{D964861E-249F-42CA-802C-7D02A7872DBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24-
{D964861E-249F-42CA-802C-7D02A7872DBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
25-
{D964861E-249F-42CA-802C-7D02A7872DBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
26-
{D964861E-249F-42CA-802C-7D02A7872DBC}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{8CE88C69-3E35-4617-A2BD-B4F66244FCA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{8CE88C69-3E35-4617-A2BD-B4F66244FCA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{8CE88C69-3E35-4617-A2BD-B4F66244FCA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{8CE88C69-3E35-4617-A2BD-B4F66244FCA0}.Release|Any CPU.Build.0 = Release|Any CPU
2727
EndGlobalSection
2828
GlobalSection(SolutionProperties) = preSolution
2929
HideSolutionNode = FALSE

SimpleWebsocketServer/Doxyfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_NAME = "SimpleWebSocketServer Library"
3838
# could be handy for archiving the generated documentation or if some version
3939
# control system is used.
4040

41-
PROJECT_NUMBER = v1.0.0
41+
PROJECT_NUMBER = v1.0.1
4242

4343
# Using the PROJECT_BRIEF tag one can provide an optional one line description
4444
# for a project that appears at the top of each page and should give viewer a
@@ -58,7 +58,7 @@ PROJECT_LOGO =
5858
# entered, it will be relative to the location where doxygen was started. If
5959
# left blank the current directory will be used.
6060

61-
OUTPUT_DIRECTORY = "G:\Github Desktop\SimpleWebSocketServer\SimpleWebsocketServer\Docs"
61+
OUTPUT_DIRECTORY = ../../SimpleWebSocketServerLocal/Docs
6262

6363
# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub-
6464
# directories (in 2 levels) under the output directory of each output format and
@@ -790,7 +790,7 @@ WARN_LOGFILE =
790790
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
791791
# Note: If this tag is empty the current directory is searched.
792792

793-
INPUT = "G:\Github Desktop\SimpleWebSocketServer\SimpleWebsocketServer\SimpleWebsocketServer"
793+
INPUT = "G:\Github Desktop\SimpleWebSocketServer\SimpleWebsocketServer"
794794

795795
# This tag can be used to specify the character encoding of the source files
796796
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
@@ -1271,7 +1271,7 @@ HTML_DYNAMIC_MENUS = YES
12711271
# The default value is: NO.
12721272
# This tag requires that the tag GENERATE_HTML is set to YES.
12731273

1274-
HTML_DYNAMIC_SECTIONS = NO
1274+
HTML_DYNAMIC_SECTIONS = YES
12751275

12761276
# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries
12771277
# shown in the various tree structured indices initially; the user can expand
@@ -1497,7 +1497,7 @@ DISABLE_INDEX = NO
14971497
# The default value is: NO.
14981498
# This tag requires that the tag GENERATE_HTML is set to YES.
14991499

1500-
GENERATE_TREEVIEW = NO
1500+
GENERATE_TREEVIEW = YES
15011501

15021502
# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that
15031503
# doxygen will group on one line in the generated HTML documentation.
@@ -1685,7 +1685,7 @@ EXTRA_SEARCH_MAPPINGS =
16851685
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
16861686
# The default value is: YES.
16871687

1688-
GENERATE_LATEX = YES
1688+
GENERATE_LATEX = NO
16891689

16901690
# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
16911691
# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of

SimpleWebsocketServer/SimpleWebsocketServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public async Task StartServerAsync()
6363
/// <summary>
6464
/// Starts running the server async.
6565
/// </summary>
66-
public void StartServer()
66+
public void StartServer()
6767
{
68-
Task.Run(async()=> await _WebSocketHttpServer.RunServer());
68+
Task.Run(async() => await _WebSocketHttpServer.RunServer());
6969
}
7070

7171
/// <summary>

SimpleWebsocketServer/WebsocketHandler.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,21 @@ public async void StartConnection(TcpClient client, string clientId, string bas
113113
public async Task<bool> StopClient(string clientId)
114114
{
115115
bool succes = false;
116+
WebSocketClientInfo key = null;
116117
foreach (var connection in _ListWithConnections)
117118
{
118119
if (connection.Key.clientId == clientId)
119120
{
120121
succes = await connection.Value.StopServerAsync();
122+
key = connection.Key;
123+
break;
121124
}
122125
}
126+
if (succes)
127+
{
128+
_ListWithConnections.Remove(key);
129+
}
130+
123131
return succes;
124132
}
125133

@@ -132,14 +140,22 @@ public async Task<bool> StopClient(string clientId)
132140
public async Task<bool> StopClient(string clientId, string reason)
133141
{
134142
bool succes = false;
143+
WebSocketClientInfo key = null;
135144
foreach (var connection in _ListWithConnections)
136145
{
137146
if (connection.Key.clientId == clientId)
138147
{
139148
succes = await connection.Value.StopServerAsync(reason);
149+
key = connection.Key;
150+
break;
140151
}
141152
}
142153

154+
if (succes)
155+
{
156+
_ListWithConnections.Remove(key);
157+
}
158+
143159
return succes;
144160
}
145161

@@ -152,7 +168,12 @@ public async Task<bool> StopAll()
152168
bool succes = false;
153169
foreach (var connection in _ListWithConnections)
154170
{
155-
succes = await connection.Value.StopServerAsync();
171+
succes = await connection.Value.StopServerAsync();
172+
}
173+
174+
if (succes)
175+
{
176+
_ListWithConnections.Clear();
156177
}
157178

158179
return succes;
@@ -171,6 +192,10 @@ public async Task<bool> StopAll(string reason)
171192
succes = await connection.Value.StopServerAsync();
172193
}
173194

195+
if (succes)
196+
{
197+
_ListWithConnections.Clear();
198+
}
174199
return succes;
175200
}
176201

@@ -216,6 +241,28 @@ public async Task<bool> SendMessage(WebSocketMessageContainer message)
216241
/// <param name="arg">Arguments containing event arguments.</param>
217242
public void OnWebsocketEvent(object sender, WebSocketEventArg arg)
218243
{
244+
245+
if (arg.isClosed)
246+
{
247+
WebSocketClientInfo key = null;
248+
bool found = false;
249+
250+
foreach (var connection in _ListWithConnections)
251+
{
252+
if (connection.Key.clientId == arg.clientId)
253+
{
254+
key = connection.Key;
255+
found = true;
256+
break;
257+
}
258+
}
259+
if (found)
260+
{
261+
_ListWithConnections.Remove(key);
262+
}
263+
}
264+
265+
219266
WebsocketEvent?.Invoke(this, arg);
220267
}
221268
}

0 commit comments

Comments
 (0)