Skip to content

Latest commit

 

History

History
98 lines (71 loc) · 1.73 KB

File metadata and controls

98 lines (71 loc) · 1.73 KB

Quick Start

Get started with Cyaim.WebSocketServer in 5 minutes.

Installation

dotnet add package Cyaim.WebSocketServer

Minimal Example

1. Create Project

dotnet new web -n WebSocketServerApp
cd WebSocketServerApp

2. Install Package

dotnet add package Cyaim.WebSocketServer

3. Configure Program.cs

using Cyaim.WebSocketServer.Infrastructure.Handlers.MvcHandler;
using Cyaim.WebSocketServer.Infrastructure.Configures;

var builder = WebApplication.CreateBuilder(args);

// Configure WebSocket route
builder.Services.ConfigureWebSocketRoute(x =>
{
    x.WebSocketChannels = new Dictionary<string, WebSocketRouteOption.WebSocketChannelHandler>()
    {
        { "/ws", new MvcChannelHandler().ConnectionEntry }
    };
    x.ApplicationServiceCollection = builder.Services;
});

builder.Services.AddControllers();

var app = builder.Build();

app.UseWebSockets();
app.UseWebSocketServer();
app.MapControllers();

app.Run();

4. Create Controller

using Cyaim.WebSocketServer.Infrastructure.Attributes;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class EchoController : ControllerBase
{
    [WebSocket]
    [HttpGet]
    public string Echo(string message)
    {
        return $"Echo: {message}";
    }
}

5. Run

dotnet run

6. Test

Connect to ws://localhost:5000/ws using a WebSocket client and send:

{
    "target": "Echo.Echo",
    "body": {
        "message": "Hello, WebSocket!"
    }
}

Next Steps