Hello,
I am working on a simple IRC-client that lets me read the twitch chat via IRC using the synchronous version of the tungstenite-crate for websocekts. I ran into an issue where the program freezes up completely in irregular intervals (1h-2h). First suspected that the connection was terminated, but the program doesn't throw a panic.
use chrono::{Local, Timelike};
use std::net::TcpStream;
use std::thread;
use std::time::Duration;
use tungstenite::connect;
use tungstenite::stream::MaybeTlsStream;
use tungstenite::WebSocket;
use url::Url;
fn main() {
irc_handler();
}
fn irc_handler() {
loop {
let mut wait_time = 1u64;
print!("{}: appemt to connect\n", print_time());
match connect(Url::parse("ws://irc-ws.chat.twitch.tv:80").unwrap()) {
Ok(x) => {
print!("{}: connected\n", print_time());
// reset wait time
wait_time = 1;
irc(x.0)
}
Err(_) => {
// double the sleep time before reconnecting again
wait_time *= 2;
}
};
thread::sleep(Duration::from_secs(wait_time));
}
}
fn irc(mut socket: WebSocket<MaybeTlsStream<TcpStream>>) {
// join as anon user
socket
.write_message(tungstenite::Message::Text("NICK justinfan12345\r\n".into()))
.unwrap();
// join a channel
socket
.write_message(tungstenite::Message::Text("JOIN #forsen\r\n".into()))
.unwrap();
socket.write_pending().unwrap();
loop {
match socket.read_message() {
Ok(recv) => {
// multiple twitch-messages in one irc-message
for msg in recv.to_text().unwrap().split("\r\n") {
if msg.trim().is_empty() {
continue;
}
if msg == "PING :tmi.twitch.tv" {
socket
.write_message(tungstenite::Message::Text(
"PONG :tmi.twitch.tv\r\n".into(),
))
.unwrap();
socket.write_pending().unwrap();
print!("{}: PING PONG\n", print_time());
continue;
}
print!("{} msg: {}\n", print_time(), msg);
}
}
Err(e) => {
match e {
// TODO: handle error types
_ => {
print!("{:?}", e);
}
}
}
}
}
}
fn print_time() -> String {
let date = Local::now();
format!("{}:{}:{}", date.hour(), date.minute(), date.second())
}
At this point, i think i have flaws in my understanding of either the tungstenite-crate or the IRC-protocol.
I would greatly appreciate any assistance you can provide regarding this issue. Thank you in advance for your support!