Comments (4)
Thank you for raising this. The documentation is not clear on this and I will update it.
What you need to do is to listen for the exception in your subscription. Like this:
_subscribeToMessagesReceived = websocketClient.ObserveTextMessagesReceived.Subscribe(
msg =>
{
System.Console.WriteLine($"Reply from test server: {msg}");
},
ex =>
{
System.Console.WriteLine(ex.Message);
},
() =>
{
System.Console.WriteLine($"Subscription Completed");
});
In the above the subscription listens to three things:
- The messages
- Exceptions
- Completion
This is the pattern used with Rx.
Please let me know if this helps?
from websocketclientlite.pcl.
It was very helpful, thank you.
The exception does come now, terrific.
If I leave the network disconnected what I see is:
- I'll get one exception from the Rx pattern above. Subsequent sends do not seem to generate it as per the pattern above, and result in my code needing to catch it via try/catch.
- when the network becomes available again, sends won't resume, they continue to fail
- what I've done is caught the exception as per your code above, then prevented the code from doing future sends until I've reconnected by creating a new MessageWebSocketRx
It seems to work, but it's not pretty :).
Again, thank you so much for the quick reply today. It definitely helped
from websocketclientlite.pcl.
Great to hear.
What you describe makes perfect sense.
When the connection fails you need to re-connect it before you try sending something new. Not sure you need to create a new instance of the MessageWebSocketRx object.
From the moment the connection fails to you receive an exception via Rx there is a time gap. In mine testing it was 20 seconds. This makes sense as the client don't know that the connection is gone. It has to timeout first.
from websocketclientlite.pcl.
Regarding looking pretty. I would it something like this:
static void Main(string[] args)
{
var outerCancellationSource = new CancellationTokenSource();
Task.Run(() => StartWebSocketAsyncWithRetry(outerCancellationSource), outerCancellationSource.Token);
System.Console.WriteLine("Waiting...");
System.Console.ReadKey();
outerCancellationSource.Cancel();
_subscribeToMessagesReceived.Dispose();
}
private static async Task StartWebSocketAsyncWithRetry(CancellationTokenSource outerCancellationTokenSource)
{
while (!outerCancellationTokenSource.IsCancellationRequested)
{
var innerCancellationSource = new CancellationTokenSource();
await StartWebSocketAsync(innerCancellationSource);
while (!innerCancellationSource.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromSeconds(10), innerCancellationSource.Token);
}
// Wait 5 seconds before trying again
await Task.Delay(TimeSpan.FromSeconds(5), outerCancellationTokenSource.Token);
}
}
private static async Task StartWebSocketAsync(CancellationTokenSource innerCancellationTokenSource)
{
using (var websocketClient = new MessageWebSocketRx())
{
_subscribeToMessagesReceived = websocketClient.ObserveTextMessagesReceived.Subscribe(
msg =>
{
// Msg receive code goes here
},
ex =>
{
// Cancel the inner loop when exception
innerCancellationTokenSource.Cancel();
},
() =>
{
// Cancel the inner loop when complete
innerCancellationTokenSource.Cancel();
});
....
See example here
from websocketclientlite.pcl.
Related Issues (20)
- Allow setting HTTP headers for handshake. HOT 1
- Subsequent connections from same client instance fail. HOT 2
- CancellationToken in develop branch HOT 7
- ConnectionTimeout - Slack HOT 24
- Message Size greater than 126 Characters HOT 3
- TLS Cipher Suite HOT 2
- The BeginWrite method cannot be called when another write operation is pending HOT 6
- Unable to be portable due to System.Reactive.Windows.Threading HOT 2
- Ignore invalid ssl cert fails HOT 2
- Visual Studio 2015 Update 3 with Xamarin HOT 3
- Cannot install nuget package for 4.1.2 in .NET Standard 2.0 project in VS 2017 HOT 1
- Sec-WebSocket-Protocol is aggregated wrong HOT 1
- Doesn't connect with proxy HOT 2
- CreateObservableMessageReceiver TimeOut HOT 1
- Could not load file or assembly ISocketLite.PCL Version=4.2.1.0 HOT 1
- Question : WebsocketClientLite versus the Microsoft client for performance HOT 1
- NuGet package versions 7.0.0 and 7.0.1 actually contain version 6.4.0 HOT 2
- Feature request: Transfer ownership of TcpClient to WebsocketClientLite library HOT 4
- IsConnected always true. HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from websocketclientlite.pcl.