-
Notifications
You must be signed in to change notification settings - Fork 76
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
Implement shared custom data (re-worked) based on the previous work from kitgxrl #435
Open
thebashpotato
wants to merge
4
commits into
1c3t3a:main
Choose a base branch
from
thebashpotato:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a2e42de
Add transmitter sender data layer for asynchronous client
thebashpotato f828ec3
Fixed all clippy warnings.
thebashpotato b386b0c
Add transmitter sender data layer for raw client
thebashpotato 82d5703
Refactor example code and documentation.
thebashpotato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
[workspace] | ||
members = ["engineio", "socketio"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,7 @@ impl Debug for Client { | |
} | ||
} | ||
|
||
#[cfg(all(test))] | ||
#[cfg(test)] | ||
mod test { | ||
|
||
use super::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#![allow(clippy::module_inception)] | ||
mod client; | ||
pub use client::Iter; | ||
pub use {client::Client, client::ClientBuilder, client::Iter as SocketIter}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use futures_util::future::{BoxFuture, FutureExt}; | ||
use rust_socketio::{ | ||
asynchronous::{Client as SocketIOClient, ClientBuilder as SocketIOClientBuilder}, | ||
Error as SocketIOError, Payload, | ||
}; | ||
use serde_json::{json, Value}; | ||
use std::sync::{mpsc, Arc}; | ||
use std::time::Duration; | ||
use tokio::time::sleep; | ||
|
||
type JsonValues = Vec<Value>; | ||
|
||
fn test_event_handler<'event>(payload: Payload, socket: SocketIOClient) -> BoxFuture<'event, ()> { | ||
async move { | ||
if let Payload::Text(values) = payload { | ||
match socket.try_transmitter::<mpsc::Sender<JsonValues>>() { | ||
Ok(tx) => { | ||
tx.send(values.to_owned()).map_or_else( | ||
|err| eprintln!("{}", err), | ||
|_| println!("Data transmitted successfully"), | ||
); | ||
} | ||
Err(err) => { | ||
eprintln!("{}", err); | ||
} | ||
} | ||
} | ||
} | ||
.boxed() | ||
} | ||
|
||
fn error_event_handler<'event>(payload: Payload, _: SocketIOClient) -> BoxFuture<'event, ()> { | ||
async move { eprintln!("Error: {:#?}", payload) }.boxed() | ||
} | ||
|
||
struct ComplexData { | ||
/// There should be many more fields below in real life, | ||
/// probaly wrapped in Arc<Mutex<T>> if you're writing a more serious client. | ||
data: String, | ||
} | ||
|
||
struct TransmitterClient { | ||
receiver: mpsc::Receiver<JsonValues>, | ||
complex: ComplexData, | ||
client: SocketIOClient, | ||
} | ||
|
||
impl TransmitterClient { | ||
async fn connect(url: &str) -> Result<Self, SocketIOError> { | ||
let (sender, receiver) = mpsc::channel::<JsonValues>(); | ||
|
||
let client = SocketIOClientBuilder::new(url) | ||
.namespace("/admin") | ||
.on("test", test_event_handler) | ||
.on("error", error_event_handler) | ||
.transmitter(Arc::new(sender)) | ||
.connect() | ||
.await?; | ||
|
||
Ok(Self { | ||
client, | ||
receiver, | ||
complex: ComplexData { | ||
data: String::from(""), | ||
}, | ||
}) | ||
} | ||
|
||
async fn get_test(&mut self) -> Option<String> { | ||
match self.client.emit("test", json!({"got ack": true})).await { | ||
Ok(_) => { | ||
match self.receiver.recv() { | ||
Ok(values) => { | ||
// Json deserialization and parsing business logic should be implemented | ||
// here to avoid over-complicating the handler callbacks. | ||
if let Some(value) = values.first() { | ||
if value.is_string() { | ||
self.complex.data = String::from(value.as_str().unwrap()); | ||
return Some(self.complex.data.clone()); | ||
} | ||
} | ||
None | ||
} | ||
Err(err) => { | ||
eprintln!("Transmission buffer is probably full: {}", err); | ||
None | ||
} | ||
} | ||
} | ||
Err(err) => { | ||
eprintln!("Server unreachable: {}", err); | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
match TransmitterClient::connect("http://localhost:4200/").await { | ||
Ok(mut client) => { | ||
if let Some(test_data) = client.get_test().await { | ||
println!("test event data from internal transmitter: {}", test_data); | ||
} | ||
} | ||
Err(err) => { | ||
eprintln!("Failed to connect to server: {}", err); | ||
} | ||
} | ||
|
||
sleep(Duration::from_secs(2)).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use rust_socketio::{ | ||
client::Client as SocketIOClient, ClientBuilder as SocketIOClientBuilder, | ||
Error as SocketIOError, Payload, RawClient, | ||
}; | ||
use serde_json::{json, Value}; | ||
use std::sync::{mpsc, Arc}; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
|
||
type JsonValues = Vec<Value>; | ||
|
||
fn test_event_handler(payload: Payload, socket: RawClient) { | ||
if let Payload::Text(values) = payload { | ||
match socket.try_transmitter::<mpsc::Sender<JsonValues>>() { | ||
Ok(tx) => { | ||
tx.send(values.to_owned()).map_or_else( | ||
|err| eprintln!("{}", err), | ||
|_| println!("Data transmitted successfully"), | ||
); | ||
} | ||
Err(err) => { | ||
eprintln!("{}", err); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn error_event_handler(payload: Payload, _: RawClient) { | ||
eprintln!("Error: {:#?}", payload); | ||
} | ||
|
||
struct ComplexData { | ||
/// There should be many more fields below in real life, | ||
/// probaly wrapped in Arc<Mutex<T>> if you're writing a more serious client. | ||
data: String, | ||
} | ||
|
||
struct TransmitterClient { | ||
client: SocketIOClient, | ||
receiver: mpsc::Receiver<JsonValues>, | ||
complex: ComplexData, | ||
} | ||
|
||
impl TransmitterClient { | ||
fn connect(url: &str) -> Result<Self, SocketIOError> { | ||
let (sender, receiver) = mpsc::channel::<JsonValues>(); | ||
|
||
let client = SocketIOClientBuilder::new(url) | ||
.namespace("/admin") | ||
.on("test", test_event_handler) | ||
.on("error", error_event_handler) | ||
.transmitter(Arc::new(sender)) | ||
.connect()?; | ||
|
||
Ok(Self { | ||
client, | ||
receiver, | ||
complex: ComplexData { | ||
data: "".to_string(), | ||
}, | ||
}) | ||
} | ||
|
||
fn get_test(&mut self) -> Option<String> { | ||
match self.client.emit("test", json!({"got ack": true})) { | ||
Ok(_) => { | ||
match self.receiver.recv() { | ||
Ok(values) => { | ||
// Json deserialization and parsing business logic should be implemented | ||
// here to avoid over-complicating the handler callbacks. | ||
if let Some(value) = values.first() { | ||
if value.is_string() { | ||
self.complex.data = String::from(value.as_str().unwrap()); | ||
return Some(self.complex.data.clone()); | ||
} | ||
} | ||
None | ||
} | ||
Err(err) => { | ||
eprintln!("Transmission buffer is probably full: {}", err); | ||
None | ||
} | ||
} | ||
} | ||
Err(err) => { | ||
eprintln!("Server unreachable: {}", err); | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
match TransmitterClient::connect("http://localhost:4200/") { | ||
Ok(mut client) => { | ||
if let Some(test_data) = client.get_test() { | ||
println!("test event data from internal transmitter: {}", test_data); | ||
} | ||
} | ||
Err(err) => { | ||
eprintln!("Failed to connect to server: {}", err); | ||
} | ||
} | ||
|
||
sleep(Duration::from_secs(2)); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please keep this code in client.rs? I assume this was to fix the
client/client
clippy lint? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was to fix the lint, I figured since the lint was enabled that it just hadn't got around to being fixed yet, I can put the code back, and disable the lint.