Skip to content

Commit

Permalink
added close to connection
Browse files Browse the repository at this point in the history
  • Loading branch information
statianzo committed Oct 29, 2010
1 parent afc0718 commit 70dd168
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
5 changes: 4 additions & 1 deletion Samples/ConsoleApp/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ static void Main()
Console.WriteLine("Close!");
allSockets.Remove(socket);
};
socket.OnMessage = message => Console.WriteLine(message);
socket.OnMessage = message =>
{
Console.WriteLine(message);
};
});


Expand Down
5 changes: 2 additions & 3 deletions src/Fleck/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Receive(DataFrame frame = null)

if (Socket == null || !Socket.Connected)
{
_connection.OnClose();
_connection.Close();
return;
}

Expand Down Expand Up @@ -56,8 +56,7 @@ public void Receive(DataFrame frame = null)
}
else
{
_connection.OnClose();
Socket.Close();
_connection.Close();
}
}, null);
}
Expand Down
17 changes: 7 additions & 10 deletions src/Fleck/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ public Sender(WebSocketConnection connection)

public void Send(string data)
{
if (Socket.Connected)
{
var wrapped = DataFrame.Wrap(data);
Socket.BeginSend(wrapped, 0, wrapped.Length, SocketFlags.None, r => Socket.EndSend(r), null);
}
else
{
_connection.OnClose();
Socket.Close();
}
if (!Socket.Connected) return;
var wrapped = DataFrame.Wrap(data);
Socket.BeginSend(wrapped, 0, wrapped.Length, SocketFlags.None, r =>
{
if (Socket.Connected)
Socket.EndSend(r);
}, null);
}
}
}
7 changes: 7 additions & 0 deletions src/Fleck/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ public void StartReceiving()
{
_receiver.Receive();
}

public void Close()
{
if (!Socket.Connected) return;
OnClose();
Socket.Close();
}
}
}
5 changes: 3 additions & 2 deletions src/Fleck/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public WebSocketServer(int port, string location)
Location = location;
}

public WebSocketServer(int port, string location, string origin):this(port,location)
public WebSocketServer(int port, string location, string origin)
: this(port, location)
{
Origin = origin;
}
Expand Down Expand Up @@ -58,6 +59,7 @@ private void OnClientConnect(IAsyncResult ar)
Log.Error("Listener socket is closed");
return;
}
ListenForClients();


var shaker = new HandshakeHandler(Origin, Location)
Expand All @@ -73,7 +75,6 @@ private void OnClientConnect(IAsyncResult ar)

shaker.Shake(clientSocket);

ListenForClients();
}
}
}

0 comments on commit 70dd168

Please sign in to comment.