Horse WebSocket includes websocket server and websocket client. Websocket servers runs on Horse Server.
IHost host = Host.CreateDefaultBuilder(args)
.UseHorseWebSocketServer((context, builder) =>
{
builder.UseModelProvider<BinaryModelProvider>();
builder.AddTransientHandlers(typeof(Program));
builder.UsePort(888);
})
.Build();
host.Run();
HorseWebSocket client = new HorseWebSocket();
client.MessageReceived += (c, m) => Console.WriteLine("# " + m);
client.Connected += c => Console.WriteLine("Connected");
client.Disconnected += c => Console.WriteLine("Disconnected");
client.Connect("wss://echo.websocket.org");
while (true)
{
string s = Console.ReadLine();
client.Send(s);
}
Handler Implementation
public class ModelAHandler : IWebSocketMessageHandler<ModelA>
{
public Task Handle(ModelA model, WebSocketMessage message, IHorseWebSocket client)
{
Console.WriteLine("Model A received: " + model.Value);
return Task.CompletedTask;
}
public Task OnError(Exception exception, ModelA model, WebSocketMessage message, IHorseWebSocket client)
{
throw new NotImplementedException();
}
}
Model
[ModelType("model-a")]
public class ModelA
{
[JsonProperty("v")]
public string Value { get; set; }
}