Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WebSocket #220

Merged
merged 15 commits into from
Jul 20, 2024
Merged

feat: WebSocket #220

merged 15 commits into from
Jul 20, 2024

Conversation

kanarus
Copy link
Member

@kanarus kanarus commented Jul 20, 2024

Add ws::{WebSocketContext, WebSocket, Message} behind "ws" feature to perform WebSocket handshake, establish WebSocket connection, and run a given handler like

use ohkami::prelude::*;
use ohkami::ws::{WebSocketContext, WebSocket, Message};

async fn echo_text(c: WebSocketContext<'_>) -> WebSocket {
    c.connect(|mut ws| async move {
        while let Ok(Some(Message::Text(text))) = ws.recv().await {
            ws.send(Message::Text(text)).await.expect("Failed to send text");
        }
    })
}

#[tokio::main]
async fn main() {
    Ohkami::new((
        "/ws".GET(echo_text),
    )).howl("localhost:3030").await
}

@kanarus kanarus merged commit a18bc87 into main Jul 20, 2024
3 checks passed
@kanarus kanarus deleted the feat/websocket branch July 20, 2024 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant