Skip to content

Commit f4d889c

Browse files
authored
Merge pull request #10 from pion/rust-websocket
Add rust websocket
2 parents f24ccb3 + 6f9caef commit f4d889c

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

webrtc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080
log.Println("Channel Open")
8181
for i := 10; i < 510; i += 10 {
8282
for j := 10; j < 510; j += 10 {
83-
message := fmt.Sprintf("%03d,%03d", j, i)
83+
message := fmt.Sprintf("%d,%d", j, i)
8484
if err := channel.Send([]byte(message)); err != nil {
8585
log.Fatal(err)
8686
}

websocket/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main() {
2323
log.Println("Client Connected")
2424
for i := 10; i < 510; i += 10 {
2525
for j := 10; j < 510; j += 10 {
26-
message := fmt.Sprintf("%03d,%03d", j, i)
26+
message := fmt.Sprintf("%d,%d", j, i)
2727
if err := conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
2828
log.Fatal(err)
2929
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use std::net::TcpListener;
2+
use std::path::Path;
3+
use std::sync::Arc;
4+
use std::thread::spawn;
5+
6+
use rustls::pki_types::pem::PemObject;
7+
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
8+
use rustls::{ServerConfig, ServerConnection, StreamOwned};
9+
use tungstenite::accept;
10+
11+
/// A WebSocket echo server over TLS (wss://)
12+
fn main() {
13+
// Use fixed relative paths from this crate to the repo certs
14+
let cert_path = Path::new("../../certs/localhost.pem");
15+
let key_path = Path::new("../../certs/localhost-key.pem");
16+
17+
eprintln!("Using certificate: {}", cert_path.display());
18+
eprintln!("Using private key: {}", key_path.display());
19+
20+
let cert = CertificateDer::from_pem_file(cert_path).expect("load certs");
21+
let key = PrivateKeyDer::from_pem_file(key_path).expect("load private key");
22+
23+
// Build rustls server config (no client auth)
24+
let config = ServerConfig::builder()
25+
.with_no_client_auth()
26+
.with_single_cert(vec![cert], key)
27+
.expect("invalid cert/key");
28+
let config = Arc::new(config);
29+
30+
let listener = TcpListener::bind("127.0.0.1:8000").expect("bind 127.0.0.1:8000");
31+
eprintln!("tungstenite server listening on wss://127.0.0.1:8000");
32+
33+
for stream in listener.incoming() {
34+
match stream {
35+
Ok(stream) => {
36+
eprintln!(
37+
"incoming TCP connection from {}",
38+
stream.peer_addr().unwrap()
39+
);
40+
let cfg = Arc::clone(&config);
41+
spawn(move || {
42+
// Wrap TCP in a rustls TLS stream.
43+
let conn = match ServerConnection::new(cfg) {
44+
Ok(c) => c,
45+
Err(e) => {
46+
eprintln!("TLS ServerConnection error: {e}");
47+
return;
48+
}
49+
};
50+
let tls_stream = StreamOwned::new(conn, stream);
51+
52+
match accept(tls_stream) {
53+
Ok(mut websocket) => loop {
54+
// Send message so we can test the server performance
55+
for i in (10..=510).step_by(10) {
56+
for j in (10..=510).step_by(10) {
57+
let message = format!("{j},{i}");
58+
if let Err(e) = websocket.send(message.into()) {
59+
eprintln!("send error: {e}");
60+
break;
61+
}
62+
std::thread::sleep(std::time::Duration::from_millis(1));
63+
}
64+
}
65+
// break after one batch for demo purposes
66+
eprintln!("Completed one batch of messages, closing connection.");
67+
break;
68+
},
69+
Err(e) => {
70+
eprintln!("websocket handshake failed: {e}");
71+
eprintln!("Hint: Ensure your client connects with wss://localhost:8000 and trusts the local certificate in certs/.");
72+
}
73+
}
74+
});
75+
}
76+
Err(e) => eprintln!("incoming connection error: {e}"),
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)