How can I close a WebSocket channel/connection from server side correctly? If I use a ctx.getChannel().close(), the onerror in the Brwoser (Firefox 9) is thrown:
The connection to ws://localhost:8080/websocket was interrupted while the page was loading
I also tried to send a CloseWebSocketFrame within the channelClosed-method in the WebSocketServerHandler:
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
CloseWebSocketFrame close = new CloseWebSocketFrame();
ctx.getChannel().write(close);
}
This throws an ClosedChannelException (maybe related to this?).
You have to do this:
ch.write(new CloseWebSocketFrame());
then the server will close the connection. If the connection is not closed soon enough, you can call ch.close().
How about
ctx.getChannel().write(new CloseWebSocketFrame()).addListener(ChannelFutureListener.CLOSE);
That should send the CloseWebSocketFrame and then close the channel after that.
What version are you using ?
Have you tried todo this:
ctx.getChannel().write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
?
I encountered the same problem(close a WebSocket channel/connection from server side ), which was solved in this way:
ctx.getChannel().writeAndFlush(new CloseWebSocketFrame(1000, "Server closed connection."));
Related
I´m getting this error exactly how the exception message says.
If a mobile client loses connection before the proxy.Invoke() result arrive, the exception is raised. That´s ok, but I need to catch this to avoid an app crash.
I try/catch all the proxy.Invoke() and proxy.Invoke<T>() calls, with no effect though.
How can I catch that exception?
Note: I´m using SignalR client 2.2.0 in a Xamarin client (PCL)
If you are calling your proxy.Invoke() without await proxy.Invoke() then the exception won't bubble up from the Invoke task to your executing code.
I've had to deal with this error before (with Xamarin in a PCL), and simply executing my Invoke like such worked for me:
try
{
await hubProxy.Invoke("SomeMethod", args);
}
catch (InvalidOperationException ex)
{
// Do what you need to with the exception
}
There is also a conversation about it here on the SignalR GitHub.
I downloaded the red5-recorder (http://www.red5-recorder.com/) , which fails to allow me to start recording. After debugging I found that the netconnection, needed to record to a media server, created does not fire a NetStatusEvent event, so essentially it fails silently. I have implemented the connection with the following minimal working example:
trace("make net connection");
nc = new NetConnection();
nc.client = { onBWDone: function():void{ trace("bandwidth check done.") } };
trace("add event listener");
nc.addEventListener(NetStatusEvent.NET_STATUS, function(event:NetStatusEvent) {
trace("handle");
});
trace("connect!");
nc.connect("rtmp://localshost/oflaDemo/test/");
trace("connect done");
The output of this piece of code is:
make net connection
add event listener
connect!
connect done
The actionscript api states that the connect-call always fires such an event:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetConnection.html#includeExamplesSummary
Moreover, the netconnection is not 'connected' (a state of the NetConnection object) 10 seconds after the call. I also took a look at this: NetConnect fails silently in Flash when called from SilverLight But the fix suggested by the author, swapping rtmp and http in the connection uri, do not work. Also, I tested the uri and in fact the exact same code sniplet in a personal project, where it did work. I just can not seem to find why connecting to a media server fails silently in the red5-recorder project.
The awkward part is that if I pass some random string as a conenction uri, still nothing happens (no event, no exception, no crash). Also not setting nc.client becore nc.connect(), which caused exceptions in my experience, did not cause exceptions.
Any suggestions are welcome.
You are setting the address to localshost instead localhost.
nc.connect("rtmp://localshost/oflaDemo/test/");
Correct address:
nc.connect("rtmp://localhost/oflaDemo/test/");
Let's say I have some ajax based component, whose handler in server throws for some reason an exception (e.g. programming error, can't access database). And basically server responds with internal server error or some such. How can I catch this situation in browser and display for examplen an error message somewhere.
When user clicks this link, the server will show an error page and the browser should detect based on http status code that something went wrong and somehow to react to it.
new AjaxLink<Link>("link") {
public void onClick(AjaxRequestTarget target) {
throw new RuntimeException();
}
}
See org.apache.wicket.settings.IExceptionSettings#getAjaxErrorHandlingStrategy.
There is an example of this at http://www.wicket-library.com/wicket-examples/ajax/links (failure and exception links).
Normally, Wicket Ajax classes like AjaxFallbackLink for example, have a method
updateAjaxAttributes(AjaxRequestAttributes attributes)
where you can register error handlers, that execute javascript within the browser.
Override this method, create an AjaxCallListener and call
AjaxCallListener listener = new AjaxCallListener();
listener.onFailure(ADD_JAVASCRIPT_TO_EXECUTE_HERE);
attributes.getAjaxCallListeners().add(listener);
see the documentation of
org.apache.wicket.ajax.attributes.AjaxCallListener.getFailureHandler(Component component)
I'm trying to use pusherapp here but chromium's console just gives me "WebSocket is closed before the connection is established." My JS is below, any ideas as to what might be the problem?
<script type='text/javascript'>
$(document).ready(function(){
var pusher_key = "<%= Pusher.key %>";
var pusher_channel = "thirsty-<%= Rails.env %>";
var pusher = new Pusher(pusher_key, pusher_channel);
pusher.bind('push_comment', function(content) {
comment_html = '<li><p>' + content + '</p></li>'
$(comment_html).prependTo('#comments');
});
});
</script>
The question is asking the reason for the following error:
WebSocket is closed before the connection is established.
This is actually a generic WebSocket error which is logged by the browser when an attempt is made to close the WebSocket connection (by calling webSocketInstance.close()) before the connection has even been established.
The error is triggered by the Pusher JavaScript library try to close a connection, but is caused by bad network or browser conditions (e.g. online/offline reporting). All all cases (that I'm aware of) the Pusher JavaScript library will retry connecting until those connections are resolved (e.g. the Internet connection is restored).
More information and an example in the following answer:
What does "WebSocket is closed before the connection is established" mean?
A couple of points on the code above:
the Pusher constructor has a first parameter which is the application key. The second parameter is a map of key value options. So, you shouldn't be passing in a channel name as the second parameter. More info on the Pusher constructor here
The pusher.bind call is actually deprecated (docs being updated now). For channel events you should bind directly on the channel using channel.bind and for connection events you should bind on the Pusher.connection object.
It could happen if the internet connection is too slow or disconnected.
Cross check the pusher Credentials.
It you have cluster in pusher channel app make sure you write it in you code.
I use HttpWebResponse.BeginGetResponse() method to make a request to my server. The request has AllowReadStreamBuffering set to false so that the data will not be buffered. In the "Request Complete" notification I do the following (no error handling code included):
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(result);
BinaryReader streamReader = new BinaryReader(response.GetResponseStream());
try
{
while ((readSize = streamReader.Read(buffer, 0, buffer.Length)) > 0)
{
...
}
}
catch (Exception)
{ // NEVER GET HERE!!
...
}
My problem is that I cannot detect connection errors during reading data from stream (during the while loop). The behavior is very strange: it finishes to read all data that the OS has in the cache and then returns 'readSize = 0' as in the case of "end of stream".
I was expecting streamReader.Read() to throw an IOException exception but it does not!
I tried to find a stream failed flag somewhere but it seems that there is none (Stream/BinaryReader/HttpWebResponse). Please help!
I tested this by using three scenarios and all had the same behavior:
Using the phone-to-PC USB connection
Phone connected to Internet using the PC USB connection
After few streamReader.Read() successful calls I disable the PC network card
Using WIFI connection
Phone connected to Internet using WIFI connection
After few streamReader.Read() successful calls I power off the WIFI router
Using WIFI connection
Phone connected to Internet using WIFI connection
After few streamReader.Read() successful calls I remove WAN cable (so it has no Internet access).
Thank you!
Mihai
If you are not getting an exception then one option is to check the number of bytes read against response.ContentLength