Skip to content

Commit 3277650

Browse files
authored
feat(socketio): async adapter (#395)
1 parent 9a58244 commit 3277650

File tree

63 files changed

+2098
-1303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2098
-1303
lines changed

.github/workflows/github-ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ on:
55
branches:
66
- main
77
pull_request:
8-
branches:
9-
- main
108

119
jobs:
1210
format:

crates/engineioxide/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ impl EngineIoHandler for MyHandler {
5353
let cnt = self.user_cnt.fetch_sub(1, Ordering::Relaxed) - 1;
5454
socket.emit(cnt.to_string()).ok();
5555
}
56-
fn on_message(&self, msg: Str, socket: Arc<Socket<SocketState>>) {
56+
fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<SocketState>>) {
5757
*socket.data.id.lock().unwrap() = msg.into(); // bind a provided user id to a socket
5858
}
59-
fn on_binary(&self, data: Bytes, socket: Arc<Socket<SocketState>>) { }
59+
fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<SocketState>>) { }
6060
}
6161

6262
// Create a new engineio layer

crates/engineioxide/src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
//! type Data = ();
1616
//! fn on_connect(self: Arc<Self>, socket: Arc<Socket<()>>) { }
1717
//! fn on_disconnect(&self, socket: Arc<Socket<()>>, reason: DisconnectReason) { }
18-
//! fn on_message(&self, msg: Str, socket: Arc<Socket<()>>) { }
19-
//! fn on_binary(&self, data: Bytes, socket: Arc<Socket<()>>) { }
18+
//! fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<()>>) { }
19+
//! fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<()>>) { }
2020
//! }
2121
//!
2222
//! let config = EngineIoConfig::builder()
@@ -150,12 +150,12 @@ impl EngineIoConfigBuilder {
150150
/// println!("socket disconnect {}", socket.id);
151151
/// }
152152
///
153-
/// fn on_message(&self, msg: Str, socket: Arc<Socket<()>>) {
153+
/// fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<()>>) {
154154
/// println!("Ping pong message {:?}", msg);
155155
/// socket.emit(msg).unwrap();
156156
/// }
157157
///
158-
/// fn on_binary(&self, data: Bytes, socket: Arc<Socket<()>>) {
158+
/// fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<()>>) {
159159
/// println!("Ping pong binary message {:?}", data);
160160
/// socket.emit_binary(data).unwrap();
161161
/// }

crates/engineioxide/src/engine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ mod tests {
115115
println!("socket disconnect {} {:?}", socket.id, reason);
116116
}
117117

118-
fn on_message(&self, msg: Str, socket: Arc<Socket<Self::Data>>) {
118+
fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<Self::Data>>) {
119119
println!("Ping pong message {:?}", msg);
120120
socket.emit(msg).ok();
121121
}
122122

123-
fn on_binary(&self, data: Bytes, socket: Arc<Socket<Self::Data>>) {
123+
fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<Self::Data>>) {
124124
println!("Ping pong binary message {:?}", data);
125125
socket.emit_binary(data).ok();
126126
}

crates/engineioxide/src/handler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
//! let cnt = self.user_cnt.fetch_sub(1, Ordering::Relaxed) - 1;
3131
//! socket.emit(cnt.to_string()).ok();
3232
//! }
33-
//! fn on_message(&self, msg: Str, socket: Arc<Socket<SocketState>>) {
33+
//! fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<SocketState>>) {
3434
//! *socket.data.id.lock().unwrap() = msg.into(); // bind a provided user id to a socket
3535
//! }
36-
//! fn on_binary(&self, data: Bytes, socket: Arc<Socket<SocketState>>) { }
36+
//! fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<SocketState>>) { }
3737
//! }
3838
//!
3939
//! // Create an engine io service with the given handler
@@ -60,8 +60,8 @@ pub trait EngineIoHandler: std::fmt::Debug + Send + Sync + 'static {
6060
fn on_disconnect(&self, socket: Arc<Socket<Self::Data>>, reason: DisconnectReason);
6161

6262
/// Called when a message is received from the client.
63-
fn on_message(&self, msg: Str, socket: Arc<Socket<Self::Data>>);
63+
fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<Self::Data>>);
6464

6565
/// Called when a binary message is received from the client.
66-
fn on_binary(&self, data: Bytes, socket: Arc<Socket<Self::Data>>);
66+
fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<Self::Data>>);
6767
}

crates/engineioxide/src/layer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
//! type Data = ();
1616
//! fn on_connect(self: Arc<Self>, socket: Arc<Socket<()>>) { }
1717
//! fn on_disconnect(&self, socket: Arc<Socket<()>>, reason: DisconnectReason) { }
18-
//! fn on_message(&self, msg: Str, socket: Arc<Socket<()>>) { }
19-
//! fn on_binary(&self, data: Bytes, socket: Arc<Socket<()>>) { }
18+
//! fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<()>>) { }
19+
//! fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<()>>) { }
2020
//! }
2121
//! // Create a new engineio layer
2222
//! let layer = EngineIoLayer::new(Arc::new(MyHandler));

crates/engineioxide/src/service/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
//! type Data = ();
1818
//! fn on_connect(self: Arc<Self>, socket: Arc<Socket<()>>) { }
1919
//! fn on_disconnect(&self, socket: Arc<Socket<()>>, reason: DisconnectReason) { }
20-
//! fn on_message(&self, msg: Str, socket: Arc<Socket<()>>) { }
21-
//! fn on_binary(&self, data: Bytes, socket: Arc<Socket<()>>) { }
20+
//! fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<()>>) { }
21+
//! fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<()>>) { }
2222
//! }
2323
//!
2424
//! // Create a new engine.io service that will return a 404 not found response for other requests

crates/engineioxide/src/socket.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
//! fn on_disconnect(&self, socket: Arc<Socket<SocketState>>, reason: DisconnectReason) {
4747
//! let cnt = self.user_cnt.fetch_sub(1, Ordering::Relaxed) - 1;
4848
//! }
49-
//! fn on_message(&self, msg: Str, socket: Arc<Socket<SocketState>>) {
49+
//! fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<SocketState>>) {
5050
//! *socket.data.id.lock().unwrap() = msg.into(); // bind a provided user id to a socket
5151
//! }
52-
//! fn on_binary(&self, data: Bytes, socket: Arc<Socket<SocketState>>) { }
52+
//! fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<SocketState>>) { }
5353
//! }
5454
//!
5555
//! let svc = EngineIoService::new(Arc::new(MyHandler::default()));

crates/engineioxide/src/str.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ impl From<Str> for String {
100100
unsafe { String::from_utf8_unchecked(vec) }
101101
}
102102
}
103+
impl From<Str> for Vec<u8> {
104+
fn from(value: Str) -> Self {
105+
Vec::from(value.0)
106+
}
107+
}
103108
impl Serialize for Str {
104109
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
105110
where

crates/engineioxide/tests/disconnect_reason.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ impl EngineIoHandler for MyHandler {
3939
self.disconnect_tx.try_send(reason).unwrap();
4040
}
4141

42-
fn on_message(&self, msg: Str, socket: Arc<Socket<()>>) {
42+
fn on_message(self: &Arc<Self>, msg: Str, socket: Arc<Socket<()>>) {
4343
println!("Ping pong message {:?}", msg);
4444
socket.emit(msg).ok();
4545
}
4646

47-
fn on_binary(&self, data: Bytes, socket: Arc<Socket<()>>) {
47+
fn on_binary(self: &Arc<Self>, data: Bytes, socket: Arc<Socket<()>>) {
4848
println!("Ping pong binary message {:?}", data);
4949
socket.emit_binary(data).ok();
5050
}

0 commit comments

Comments
 (0)