Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Server.cs hacktoberfest-accepted #345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Samples/ConsoleApp/Server.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;

namespace Fleck.Samples.ConsoleApp
Expand All @@ -10,7 +13,9 @@ class Server
static void Main()
{
FleckLog.Level = LogLevel.Debug;
var allSockets = new List<IWebSocketConnection>();
//Using concurrent bag instead of list
//This gives a thread - safe collection by adding locks when modifying allSockets
var allSockets = new ConcurrentBag<IWebSocketConnection>();
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.Start(socket =>
{
Expand All @@ -22,7 +27,7 @@ static void Main()
socket.OnClose = () =>
{
Console.WriteLine("Close!");
allSockets.Remove(socket);
allSockets.TryTake(out socket);
};
socket.OnMessage = message =>
{
Expand All @@ -35,7 +40,7 @@ static void Main()
var input = Console.ReadLine();
while (input != "exit")
{
foreach (var socket in allSockets.ToList())
foreach (var socket in allSockets)
{
socket.Send(input);
}
Expand Down