Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An implementation of a socket.io client written in the Rust programming language.

License

NotificationsYou must be signed in to change notification settings

1c3t3a/rust-socketio

Repository files navigation

Latest Versiondocs.rsBuild and code styleTestcodecov

Rust-socketio-client

An implementation of a socket.io client written in the rust programming language. This implementation currently supports revision 5 of the socket.io protocol and therefore revision 4 of the engine.io protocol. If you have any connection issues with this client, make sure the server uses at least revision 4 of the engine.io protocol.Information on theasync version can be found below.

Example usage

Add the following to yourCargo.toml file:

rust_socketio ="*"

Then you're able to run the following example code:

use rust_socketio::{ClientBuilder,Payload,RawClient};use serde_json::json;use std::time::Duration;// define a callback which is called when a payload is received// this callback gets the payload as well as an instance of the// socket to communicate with the serverlet callback = |payload:Payload,socket:RawClient|{match payload{Payload::String(str) =>println!("Received: {}",str),Payload::Binary(bin_data) =>println!("Received bytes: {:#?}", bin_data),}       socket.emit("test",json!({"got ack":true})).expect("Server unreachable")};// get a socket that is connected to the admin namespacelet socket =ClientBuilder::new("http://localhost:4200").namespace("/admin").on("test", callback).on("error", |err, _|eprintln!("Error: {:#?}", err)).connect().expect("Connection failed");// emit to the "foo" eventlet json_payload =json!({"token":123});socket.emit("foo", json_payload).expect("Server unreachable");// define a callback, that's executed when the ack got ackedlet ack_callback = |message:Payload, _|{println!("Yehaa! My ack got acked?");println!("Ack data: {:#?}", message);};let json_payload =json!({"myAckData":123});// emit with an acksocket.emit_with_ack("test", json_payload,Duration::from_secs(2), ack_callback).expect("Server unreachable");socket.disconnect().expect("Disconnect failed")

The main entry point for using this crate is theClientBuilder which provides a way to easily configure a socket in the needed way. When theconnect method is called on the builder, it returns a connected client which then could be used to emit messages to certain events. One client can only be connected to one namespace. If you need to listen to the messages in different namespaces you need to allocate multiple sockets.

Documentation

Documentation of this crate can be found up ondocs.rs.

Current features

This implementation now supports all of the features of the socket.io protocol mentionedhere.It generally tries to make use of websockets as often as possible. This means most timesonly the opening request uses http and as soon as the server mentions that he is able to upgrade towebsockets, an upgrade is performed. But if this upgrade is not successful or the serverdoes not mention an upgrade possibility, http-long polling is used (as specified in the protocol specs).Here's an overview of possible use-cases:

  • connecting to a server.
  • register callbacks for the following event types:
    • open
    • close
    • error
    • message
    • custom events like "foo", "on_payment", etc.
  • send JSON data to the server (viaserde_json which provides safehandling).
  • send JSON data to the server and receive anack.
  • send and handle Binary data.

This library provides an ability for being executed in an asynchronous context usingtokio asthe execution runtime.Please note that the current async implementation is still experimental, the interface can be object tochanges at any time.The asyncClient andClientBuilder support a similar interface to the sync version and livein theasynchronous module. In order to enable the support, you need to enable theasyncfeature flag:

rust_socketio = {version ="*",features = ["async"] }

The following code shows the example above in async fashion:

use futures_util::FutureExt;use rust_socketio::{    asynchronous::{Client,ClientBuilder},Payload,};use serde_json::json;use std::time::Duration;#[tokio::main]asyncfnmain(){// define a callback which is called when a payload is received// this callback gets the payload as well as an instance of the// socket to communicate with the serverlet callback = |payload:Payload,socket:Client|{asyncmove{match payload{Payload::String(str) =>println!("Received: {}",str),Payload::Binary(bin_data) =>println!("Received bytes: {:#?}", bin_data),}            socket.emit("test",json!({"got ack":true})).await.expect("Server unreachable");}.boxed()};// get a socket that is connected to the admin namespacelet socket =ClientBuilder::new("http://localhost:4200/").namespace("/admin").on("test", callback).on("error", |err, _|{asyncmove{eprintln!("Error: {:#?}", err)}.boxed()}).connect().await.expect("Connection failed");// emit to the "foo" eventlet json_payload =json!({"token":123});    socket.emit("foo", json_payload).await.expect("Server unreachable");// define a callback, that's executed when the ack got ackedlet ack_callback = |message:Payload, _:Client|{asyncmove{println!("Yehaa! My ack got acked?");println!("Ack data: {:#?}", message);}.boxed()};let json_payload =json!({"myAckData":123});// emit with an ack    socket.emit_with_ack("test", json_payload,Duration::from_secs(2), ack_callback).await.expect("Server unreachable");    socket.disconnect().await.expect("Disconnect failed");}

Content of this repository

This repository contains a rust implementation of the socket.io protocol as well as the underlying engine.io protocol.

The details about the engine.io protocol can be found here:

The specification for the socket.io protocol here:

Looking at the component chart, the following parts are implemented (Source:https://socket.io/images/dependencies.jpg):

Licence

MIT

About

An implementation of a socket.io client written in the Rust programming language.

Topics

Resources

License

Stars

Watchers

Forks

Contributors21

Languages


[8]ページ先頭

©2009-2025 Movatter.jp