Created
January 9, 2023 11:30
-
-
Save gitcrtn/0eba406b1374c82f75c57a4f39597ade to your computer and use it in GitHub Desktop.
rust_socketio example
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
[package] | |
name = "socketio-trial" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
rust_socketio = "0.4.0" |
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
// socket.io client by Rust | |
use std::sync::{Arc, Mutex}; | |
use rust_socketio::{ClientBuilder, Payload, RawClient}; | |
const URL: &str = "http://localhost:9001/"; | |
struct App { | |
pub finish: bool, | |
} | |
impl App { | |
pub fn new() -> Self { | |
App { | |
finish: false, | |
} | |
} | |
fn on_message(&mut self, payload: Payload, socket: RawClient) { | |
println!("message: {:#?}", payload); | |
socket.emit("disconnect", "received message").expect("Server unreachable"); | |
self.finish = true; | |
} | |
} | |
fn main() { | |
let app = Arc::new(Mutex::new(App::new())); | |
let event_app = app.clone(); | |
ClientBuilder::new(URL) | |
.on("open", |_, _| println!("Connected")) | |
.on("close", |_, _| println!("Disconnected")) | |
.on("hello", |msg, _| println!("hello: {:#?}", msg)) | |
.on("message", move |msg, client| { | |
event_app.lock().unwrap().on_message(msg, client); | |
}) | |
.on("error", |err, _| eprintln!("Error: {:?}", err)) | |
.connect() | |
.expect("Connection failed"); | |
loop { | |
if app.lock().unwrap().finish { | |
break; | |
} | |
} | |
} |
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
// socket.io server by Deno | |
import { serve } from "https://deno.land/std@0.150.0/http/server.ts"; | |
import { Server } from "https://deno.land/x/socket_io@0.1.1/mod.ts"; | |
const io = new Server(); | |
io.on("connection", (socket) => { | |
console.log(`socket ${socket.id} connected`); | |
socket.emit("hello", "world"); | |
socket.emit("test"); | |
socket.on("disconnect", (reason) => { | |
console.log(`socket ${socket.id} disconnected due to ${reason}`); | |
}); | |
}); | |
await serve(io.handler(), { | |
port: 9001, | |
}); |
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
#!/usr/bin/env bash | |
deno run --allow-net server.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment