57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Net.WebSockets;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Websocket.Client;
|
|||
|
|
|||
|
namespace azala.info_Scraper {
|
|||
|
class Program {
|
|||
|
private const string AZALA_INFO_URL = "wss://azala.info/main";
|
|||
|
private readonly WebsocketClient socket;
|
|||
|
|
|||
|
public Program(string url) {
|
|||
|
Console.WriteLine("Connecting to {0}", url);
|
|||
|
var uri = new Uri(url);
|
|||
|
|
|||
|
socket = new WebsocketClient(uri) {
|
|||
|
ReconnectTimeoutMs = (int)TimeSpan.FromSeconds(30).TotalMilliseconds
|
|||
|
};
|
|||
|
socket.ReconnectionHappened.Subscribe(type =>
|
|||
|
Console.WriteLine($"Reconnection happened, type: {type}"));
|
|||
|
socket.MessageReceived.Subscribe(MessageReceived);
|
|||
|
}
|
|||
|
|
|||
|
private void MessageReceived(ResponseMessage msg) {
|
|||
|
if (msg.MessageType == WebSocketMessageType.Text) {
|
|||
|
TextMessage(msg.Text);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void TextMessage(string text) {
|
|||
|
try {
|
|||
|
|
|||
|
} catch (Exception e) {
|
|||
|
Console.WriteLine("Cannot parse message (JSON expected): {0}",e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static async Task Main() {
|
|||
|
try {
|
|||
|
await new Program(AZALA_INFO_URL).Run();
|
|||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
|||
|
} catch (Exception e) {
|
|||
|
Console.WriteLine("Uncaught error:\n{0}", e);
|
|||
|
}
|
|||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
|||
|
}
|
|||
|
|
|||
|
private async Task Run() {
|
|||
|
await socket.Start();
|
|||
|
Console.ReadLine();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|