Coder Social home page Coder Social logo

neondatabase / framed-websockets Goto Github PK

View Code? Open in Web Editor NEW

This project forked from denoland/fastwebsockets

1.0 1.0 0.0 291 KB

A fast RFC6455 WebSocket implementation

Home Page: https://docs.rs/fastwebsockets/

License: Apache License 2.0

Rust 98.81% Makefile 1.19%

framed-websockets's Introduction

framed-websockets is a fast WebSocket protocol implementation.

Passes the Autobahn|TestSuite and fuzzed with LLVM's libfuzzer.

You can use it as a raw websocket frame parser and deal with spec compliance yourself, or you can use it as a full-fledged websocket client/server.

Example

use tokio::net::TcpStream;
use framed_websockets::{WebSocketServer, OpCode};
use anyhow::Result;
use futures_util::stream::TryStreamExt;
use futures_util::sink::SinkExt;

async fn handle(
  socket: TcpStream,
) -> Result<()> {
  let mut ws = WebSocketServer::after_handshake(socket);

  while let Some(frame) = ws.try_next().await? {
    match frame.opcode {
      OpCode::Close => break,
      OpCode::Text | OpCode::Binary => {
        ws.send(frame).await?;
      }
      _ => {}
    }
  }
  Ok(())
}

Fragmentation

framed_websockets will give the application raw frames with FIN set. Other crates like tungstenite which will give you a single message with all the frames concatenated.

Usage of the max_message_size with further fragment incoming messages to avoid buffering too much in memory.

permessage-deflate is not supported yet.

HTTP Upgrades

This crate supports handling server-side upgrades. This feature is powered by hyper.

use framed_websockets::upgrade::upgrade;
use http_body_util::Empty;
use hyper::{Request, body::{Incoming, Bytes}, Response};
use anyhow::Result;

async fn server_upgrade(
  mut req: Request<Incoming>,
) -> Result<Response<Empty<Bytes>>> {
  let (response, fut) = upgrade(&mut req)?;

  tokio::spawn(async move {
    let ws = fut.await;
    // Do something with the websocket
  });

  Ok(response)
}

framed-websockets's People

Contributors

littledivy avatar conradludgate avatar bartlomieju avatar mmastrac avatar erebe avatar kaniini avatar dxvid-pts avatar carrotzrule123 avatar nitn3lav avatar parastrom avatar

Stargazers

George MacKerron avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.