I'm testing SignalR (0.4.0 via nuget) and can't find any way to have the server forcefully disconnect a client. I assume I'm missing something obvious.
My test code is below and I've tried both Close() and Timeout() in pretty much every place and combination I can think of with no success. The client continues to receive pulse messages, though I do always get 2 reconnections within the first 4-5 seconds that appear to come from return Connection.Close() in OnReceivedAsync()
Server:
internal class SignalRServer
{
private Server server;
public SignalRServer()
{
server = new Server("http://localhost:13170/");
server.MapConnection<EchoConnection>("/echo");
server.Start();
Timer timer = new Timer(1000);
timer.Elapsed += OnTimer;
timer.Enabled = true;
}
void OnTimer(object sender, ElapsedEventArgs e)
{
IConnectionManager manager = server.DependencyResolver.GetService(typeof(IConnectionManager)) as IConnectionManager;
IConnection connection = manager.GetConnection<EchoConnection>();
connection.Broadcast("pulse");
connection.Close();
connection.Timeout();
}
}
internal class EchoConnection : PersistentConnection
{
protected override Task OnConnectedAsync(IRequest request, IEnumerable<string> groups, string connectionId)
{
Connection.Timeout();
Connection.Close();
return Connection.Broadcast(String.Format("{0} connection", connectionId));
}
protected override Task OnReconnectedAsync(IRequest request, IEnumerable<string> groups, string connectionId)
{
return Connection.Broadcast(String.Format("{0} reconnection", connectionId));
}
protected override Task OnReceivedAsync(string connectionId, string data)
{
Console.WriteLine(data);
Connection.Close();
Connection.Timeout();
Connection.Broadcast(data);
return Connection.Close();
}
}
Client:
internal class SignalRClient
{
private readonly Connection connection;
public SignalRClient()
{
connection = new Connection("http://localhost:13170/echo");
connection.Received += OnReceive;
connection.Closed += OnClosed;
connection
.Start()
.ContinueWith(t =>
{
if (!t.IsFaulted)
connection.Send("Hello");
else
Console.WriteLine(t.Exception);
});
Console.WriteLine(connection.ConnectionId);
}
void OnClosed()
{
// never called
connection.Stop();
}
void OnReceive(string data)
{
Console.WriteLine(data);
}
}
Sample Client output:
d7615b15-f80c-4bc5-b37b-223ef96fe96c connection
Hello
pulse
pulse
d7615b15-f80c-4bc5-b37b-223ef96fe96c reconnection
pulse
pulse
d7615b15-f80c-4bc5-b37b-223ef96fe96c reconnection
pulse
pulse
pulse
pulse
pulse
pulse
...
In .Net Core use
Context.Abort();
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.hubcallercontext.abort
Send a specific string to the client to force the disconnect:
void OnReceive(string data)
{
if(data.Equals("exit"))
{
connection.Stop();
return;
}
Console.WriteLine(data);
}
Related
I have the following code:
class MyServer
{
TcpListener myList;
Socket socket;
public MyServer(string ip)
{
this.myList = new TcpListener(ip, 12001);
this.myList.Start();
Thread t = new Thread(new ThreadStart(GetConnection));
t.Start();
}
public void GetConnection()
{
try
{
socket = myList.AcceptSocket(); //(1)
}
catch
{
Console.WriteLine("Error");
}
}
public void StopListening()
{
this.myList.Stop();
}
}
And it works well: I start the server, and if I "regret" I call StopListening() (before connection has been made!) and because I close myList, (1) is failing.
Is there any way to write this code without the try{}catch{} - rewrite GetConnection() as:
public void GetConnection()
{
while ( myList is open && there is no connection)
{
//do nothing
}
if (myList is open)
{
this.socket = myList.AcceptConnection();
}
}
or another way? Thanks.
socket = myList.AcceptSocket()
Is a blocking call, so it will throw an exception if the underlying socket stops Listening so you will need a try/catch anyway for that blocking call.
You could test:
if (myList.Server.IsBound)
{
this.socket = myList.AcceptConnection();
}
This way you wouldn't block for the next connection unless the socket was actively listening. But like I said if you want the program to be able to stop listening at some point, then this will still throw an exception when you call myList.Stop()
I have a simple application which sends and receives data.
ZSocketExample client = new ZSocketExample("127.0.0.1:5555");
client.send("test");
This is my client class:
public class ZSocketExample:IDisposable
{
public delegate void ReceiveEventHandler(object sender, SocketEventArgs e);
public event ReceiveEventHandler ReceiveEvent;
private ZmqContext zmqContext;
private ZmqSocket zmqSocket;
private string host;
private bool isRunning;
private bool disposed = false;
public ZSocketExample(string host)
{
try
{
zmqContext = ZmqContext.Create();
zmqSocket = zmqContext.CreateSocket(SocketType.DEALER);
ZHelpers.SetID(zmqSocket, Encoding.UTF8);
zmqSocket.Connect(host);
this.isRunning = true;
zmqSocket.ReceiveReady += new EventHandler<SocketEventArgs>(zmqSocket_ReceiveReady);
zmqSocket.SendReady += new EventHandler<SocketEventArgs>(zmqSocket_SendReady);
Poller poller = new Poller(new List<ZmqSocket> { zmqSocket });
while (isRunning)
{
poller.Poll(TimeSpan.FromSeconds(5));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void zmqSocket_ReceiveReady(object sender, SocketEventArgs e)
{
Console.WriteLine("Receive Ready");
}
void zmqSocket_SendReady(object sender, SocketEventArgs e)
{
Console.WriteLine("Send Ready");
}
public void send(string msg)
{
zmqSocket.Send(msg, Encoding.UTF8);
if (ReceiveEvent != null)
ReceiveEvent(this, null);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
close();
}
disposed = true;
}
public void close()
{
isRunning = false;
zmqSocket.Linger = TimeSpan.FromSeconds(1);
zmqSocket.Close();
zmqContext.Terminate();
}
}
But somehow it doesnot send or receive. Can someone tell me what do I do wrong? And this example blocks the main application. how can I make it non blocking?
ZeroMQ socket is not thread safe, you can't use it from multiple threads without using some kind of synchronization.
In your example you call the close which set a variable and then immediately close the socket, this is wrong, you should close the socket when you are out of the while loop.
Regarding receive/send ready, you rarely need to register for send ready, send ready will let you know when you can send a message, in the dealer socket if you are connected you always will be ready to send (unless the highwater has reached).
Receive Ready will be invoked when there is message ready to be received, if other side send you a message the receive ready will be invoked.
For the last part, blocking, you need a dedicate thread to handle zeromq sockets, you can have one thread that handle multiple sockets using the poller.
So i know there are a lot of articles out there on this topic and i did read a lot of them i would say but for some reason im sure my code is not doing what it is supposed to do.
I want to close a connection between my Server and my Client.
Now on the serverside i start the disconnect with this code
public void shutdown()
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
}
And on the Client side i have some troubles understanding how i get to the disconnect what i believe is happening is the following: in my async receive callback i should get an error since the server started a shutdown sequence and i have to handle that (right???) so my code for the client looks like this:
ReceiveCallback:
private void ReceiveCallback(IAsyncResult result)
{
int bytesRecieved = 0;
byte[] tempBuff;
//Try to receive But if a Socket error occures disconnect otherwise start Receiving again
try
{
bytesRecieved = _socket.EndReceive(result);
}
catch (SocketException sockEx)
{
Disconnect(sockEx);
return;
}
catch (ObjectDisposedException disposeEx)
{
Disconnect(disposeEx);
return;
}
catch (Exception ex)
{
StartReceive();
return;
}
if (bytesRecieved == 0)
{
StartReceive();
return;
}
tempBuff = new byte[bytesRecieved];
Buffer.BlockCopy(_buffer, 0, tempBuff, 0, bytesRecieved);
StartReceive();
_packHandler.handlePacket(tempBuff);
}
Disconnect:
public void Disconnect()
{
if (!_socket.Connected)
{
return;
}
_socket.BeginDisconnect(false, DisconnectCallback, null);
}
DisconnectCallback
private void DisconnectCallback(IAsyncResult result)
{
_socket.EndDisconnect(result);
_socket.Close();
}
(The Disconnect Method is overloaded so if i get an exception it puts up a messageBox and then also calls Disconnect. Just so i know what happened.)
Where am i wrong and what can i improve uppon ???
I tried the code and it seemed to work but i then looked with netstat if all sockets are closed and the client socket was not. It was in FIN_WAIT_2 which means that it (or the server???) did not yet send the FIN packet right ?
Oh and then i tried it again with this line changed:
if (bytesRecieved == 0)
{
StartReceive();
return;
}
TO
if (bytesRecieved == 0)
{
Disconnect;
return;
}
which then threw an exception on the serverside and on the clientside the client said that the connection was closed by the server ???
EDIT: Even when i have closed both Programs Netstat still shows the port in a WAITING status. what does that tell me ?
Your normal disconnect has a shutdown which will clear the socket so it can disconnect properly but your async style never calls shutdown. I added it in a convenient location below.
public void Disconnect()
{
if (!_socket.Connected)
{
return;
}
_socket.Shutdown(SocketShutdown.Both); // Make sure to do this
_socket.BeginDisconnect(false, DisconnectCallback, null);
}
EDIT:
From the sounds of it you don't have a reason to be using the Async Method? The async methods are so you can send data in a separate execution thread freeing up your thread to do some data processing for instance while that occurs.
I don't see any processing going on so I suggest you change disconnect like this and see if it resolves the problem. Because I don't think you are waiting on the Async methods which will not work out well.
public void Disconnect()
{
if (!_socket.Connected)
{
return;
}
shutdown(); //Your standard disconnect that you showed up top. Scoping might be required.
}
A bit of data on Async can be scrounged up here: http://msdn.microsoft.com/en-us/library/38dxf7kt(v=vs.110).aspx
Important is:
If a server starts a shutdown sequence you DO have to handle it
Both sides have to call shutdown on their socket
You need a way to notice the disconnect (it does not give you an error, or least it didnt for me)
Therefor I created my own class customSocket which inherits from Socket
public class customSocket : Socket
{
#region Properties
private readonly Timer _timer;
private const int _interval = 1000;
private bool Connected
{
get
{
bool part1 = Poll(1000, SelectMode.SelectRead);
bool part2 = (Available == 0);
if (part1 && part2)
return false;
else
return true;
}
}
public bool EventsEnabled
{
set
{
if (value)
{
_timer.Start();
}
else
_timer.Stop();
}
}
#endregion
#region Constructors
public customSocket(AddressFamily addressFamily, SocketType sockType, ProtocolType protocolType)
: base(addressFamily, sockType, protocolType)
{
_timer = new Timer { Interval = _interval };
_timer.Elapsed += TimerTick;
}
public customSocket(SocketInformation sockInfo)
: base(sockInfo)
{
_timer = new Timer { Interval = _interval };
_timer.Elapsed += TimerTick;
}
#endregion
#region Events
public event EventHandler<EventArgs> Socket_disconected;
public void Raise_Socket_disconnected()
{
EventHandler<EventArgs> handler = Socket_disconected;
if (handler != null)
{
handler(this,new EventArgs());
}
}
private void TimerTick(object sender, EventArgs e)
{
if (!Connected)
{
Raise_Socket_disconnected();
}
}
#endregion
}
This version of a socket has an Event for a disconnect.
Now if you create an instance of your socket class you have to connect the handler and set the EventsEnabled true.
This handler then calls the shutdown and your socket does not stay in FIN_WAIT_2
I'm wanting to stop a System.Timers.Timer that is running in a SignalR hub after a client closes a window/tab containing the active connection.
I have tried sending a bool value to the server by calling server code to notify the server the client is still connected or not, but it's not currently working.
window.onbeforeunload = function () {
profile.server.setIsConnected(false);
};
Server Side:
public ProfileHub()
{
timer = new Timer(15000);
timer.Elapsed += (sender, e) => { timer_Elapsed(sender, e, _isActive); };
timer.AutoReset = false;
}
[Authorize]
private void timer_Elapsed(object sender, ElapsedEventArgs e, bool active)
{
timer.Stop();
if (active)
{
System.Diagnostics.Debug.WriteLine("Timer Started");
timer.Start();
}
else
{
System.Diagnostics.Debug.WriteLine("Timer Stopped");
return;
}
// process code
}
[Authorize]
public void SetIsActive(bool isActive)
{
_isActive = isActive;
}
Is this possible and am I on the right track? I suspect it has something to do with the anonymous delegate for timer.Elapsed, but I'm not entirely sure.
SignalR has OnConnected, OnDisconnected, and OnReconnected that are called every time the client does one of those actions. You can simply override them:
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected()
{
//custom logic here
return base.OnDisconnected();
}
public override Task OnReconnected()
{
return base.OnReconnected();
}
I've found them to be extremely useful also for debugging purposes. If you're wanting to set a timer for each person, you should use some sort of connectionMapping along with the above functions to keep track of your users.
You should use the method OnDisconnected instead of the timer. Here's the official documentation, but the framework gives you events when a connected client disconnects or reconnects.
Socket programming, ASP.Net C#, VS2008
I am trying to determine whether the status of remote(client) is connected or disconnected on every 5 second through timer control. Whenever I start the server first time I getting the socket status is connected=true. But next time I getting socket status is null.
Note: I am Accessing a Socket status from class to Aspx page through creating object of a class.
This is my code:
Aspx page:
protected void Timer1_Tick1(object sender, EventArgs e)
{
Communication obj=new communication();
bool Rexits=obj.listen();
if(Rexits)
Response.write("Remote is On");
else
Response.write("Remote is Off");
}
**************************************************************************
Communication class:
Socket listener=new Socket();
Socket connection;
public bool listen()
{
listener.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
bool RemoteExits = false;
try
{
if (connection== null)
{
listener.Bind(endPoint);
listener.Listen(pendingConnectionQueueSize);
listener.BeginAccept(AcceptConnection, null);
RemoteExits = IsConnected(connection);
}
return RemoteExits;
}
catch (Exception ex)
{
a = IsConnected(connection);
return RemoteExits;
}
}
//callback method
protected void AcceptConnection(IAsyncResult res)
{
// Make sure listener doesn't go null on us.
lock (this)
{
connection = listener.EndAccept(res);
listener.BeginAccept(AcceptConnection, null);
}
// Close the connection if there are no handlers to accept it!
if (Connected == null)
{
connection.Close();
}
else
{
TcpServer tc=new TcpServer();
Clifton.TcpLib.ConnectionState cs=new
Clifton.TcpLib.ConnectionState(connection,cs);
OnConnected(new TcpServerEventArgs(cs));
}
}
//Poll Mehod
public static bool IsConnected(Socket client)
{
try
{
bool connected = !(client.Poll(1, SelectMode.SelectRead) &&
client.Available == 0);
return connected;
}
catch
{
return false;
}
}