@@ -12,6 +12,7 @@ use std::collections::HashMap;
1212use std:: pin:: Pin ;
1313use std:: sync:: Mutex ;
1414use std:: task:: { Context , Poll } ;
15+ use std:: time:: Duration ;
1516
1617use actix_cors:: Cors ;
1718use actix_web:: web:: { Bytes , Data , Path } ;
@@ -20,6 +21,7 @@ use clap::{crate_version, clap_app};
2021use futures:: Stream ;
2122use serde:: { Deserialize , Serialize } ;
2223use tokio:: sync:: broadcast:: { channel, Receiver , Sender } ;
24+ use tokio:: time:: { interval_at, Instant } ;
2325
2426
2527#[ actix_rt:: main]
@@ -52,6 +54,7 @@ async fn main() -> std::io::Result<()> {
5254 . bind ( format ! ( "{}:{}" ,
5355 matches. value_of( "HOST" ) . unwrap_or( "127.0.0.1" ) ,
5456 matches. value_of( "PORT" ) . unwrap_or( "8080" ) ) ) ?
57+ . maxconn ( 500000 )
5558 . run ( )
5659 . await
5760}
@@ -96,6 +99,24 @@ struct SpotifyData {
9699 msg : String ,
97100}
98101
102+ struct Broadcaster {
103+ senders : Vec < Sender < Bytes > > ,
104+ num_clients : u32 ,
105+ }
106+
107+ impl Broadcaster {
108+ fn new ( ) -> Broadcaster {
109+ Broadcaster {
110+ senders : Vec :: new ( ) ,
111+ num_clients : 1 ,
112+ }
113+ }
114+
115+ // fn send(&msg) {
116+ //
117+ // }
118+ }
119+
99120struct BroadcasterMap {
100121 broadcasters : HashMap < String , Sender < Bytes > > ,
101122 channel_num : usize ,
@@ -105,20 +126,30 @@ impl BroadcasterMap {
105126 fn new ( ) -> Self {
106127 BroadcasterMap {
107128 broadcasters : HashMap :: < String , Sender < Bytes > > :: new ( ) ,
108- channel_num : 50000
129+ channel_num : 500000
109130 }
110131 }
111132
112133 fn create ( ) -> Data < Mutex < Self > > {
113134 Data :: new ( Mutex :: new ( BroadcasterMap :: new ( ) ) )
114135 }
115136
137+ fn spawn_ping ( tx : Sender < Bytes > ) {
138+ actix_rt:: spawn ( async move {
139+ let mut task = interval_at ( Instant :: now ( ) , Duration :: from_secs ( 10 ) ) ;
140+ while let _ = task. tick ( ) . await {
141+ tx. send ( Bytes :: from ( "data: ping\n \n " ) ) ;
142+ }
143+ } ) ;
144+ }
145+
116146 fn new_client ( & mut self , room : & str ) -> Client {
117147 let s = ( & room) . to_string ( ) ;
118148 match self . broadcasters . get_mut ( & s) {
119149 Some ( broadcaster) => Client ( broadcaster. subscribe ( ) ) ,
120150 None => {
121151 let ( tx, rx) = channel ( self . channel_num ) ;
152+ // BroadcasterMap::spawn_ping(tx.clone());
122153 self . broadcasters . insert ( s. to_string ( ) , tx) ;
123154 Client ( rx)
124155 }
0 commit comments