Smee.IO Client library implementation for Dotnet.
A Nuget package will soon be released.
More details on the official project can be found at https://github.com/probot/smee.io/. Please note that I am not a maintainer of Smee.IO.
It is really simple, you simply have to connect to the Smee URL and then attach your listeners. Since Smee.IO uses EventSources sent through HTTP Streaming, the site or server, currently push the notifications. This work in a similar way as the WebSocket except without a TCP socket.
Within a Web Application, I recommend you to use the BackgroundService
Host Service in order to receive the requests. This will create a service running beind the scene and then it will trigger action within your application. The code will be shortened since it will duplicate the console code.
public class WatchMySmee : BackgroundService
{
private readonly ILogger<WatchMySmee> _logger;
public WatchMySmee(ILogger<WatchMySmee> logger) {
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
_logger.LogWarning("Start my smee");
var smeeCli = new SmeeClient(new Uri("https://smee.io/bbD7RyIMjQ2LCV9"));
smeeCli.OnConnect += (sender, args) => _logger.LogInformation("Connected to SMEE.io");
smeeCli.OnMessage += (sender, smeeEvent) => {
_logger.LogInformation($"Message: {JsonConvert.SerializeObject(smeeEvent)}")
};
//////////////////////////////////////////
// See Console application for other events
//////////////////////////////////////////
await smeeCli.StartAsync(stoppingToken); // Token is optional here
}
// etc.
}
var smeeUri = new Uri("https://smee.io/bbD7RyIMjQ2LCV9"); // Random URI
var smeeCli = new SmeeClient(smeeUri);
// Register to the existing events, you should pick only what you require.
smeeCli.OnConnect += (sender, a) => Console.WriteLine($"Connected to Smee.io ({smeeUri})");
smeeCli.OnDisconnect += (sender, a) => Console.WriteLine($"Disconnected from Smee.io ({smeeUri})");
smeeCli.OnMessage += (sender, smeeEvent) =>
{
Console.Write("Message received: ");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write(JsonConvert.SerializeObject(smeeEvent)); // This is a typed object.
Console.ResetColor();
Console.WriteLine();
};
smeeCli.OnPing += (sender, a) => Console.WriteLine("Ping from Smee");
smeeCli.OnError += (sender, e) => Console.WriteLine("Error was raised (Disconnect/Anything else: " + e.Message);
// Ctrl-c from a main/console will be caught to cancel the event.
Console.CancelKeyPress += (sender, eventArgs) =>
{
source.Cancel();
eventArgs.Cancel = true;
};
await smeeCli.StartAsync(token); // Token is optional here
Currently, here are the existing events:
- OnMessage
- OnError (Usually happens at the same time as a disconnection)
- OnPing
- OnConnect
- OnDisconnect (Usually happens at the same time as an error)
I wrote a small demo application (console) in order to show that it actually works. Not all the possible cases have been tested and some errors might rise. In such case, please simply create an issue.
The demo project is included in this repository, simply navigate to the src folder or open directly the solution.
None at the moment.
Some documentation about the SSE (Server Side Events) can be found at :
- https://hpbn.co/server-sent-events-sse/
- https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
MIT