@@ -20,6 +20,8 @@ pub enum HttpServerRequest {
2020 WebSocketOpen {
2121 path : String ,
2222 channel_id : u32 ,
23+ #[ serde( default ) ]
24+ source_socket_addr : Option < String > ,
2325 } ,
2426 /// Processes can both SEND and RECEIVE this kind of [`crate::Request`]
2527 /// (send as [`HttpServerAction::WebSocketPush`]).
@@ -300,6 +302,8 @@ pub struct HttpServer {
300302 ws_paths : HashMap < String , WsBindingConfig > ,
301303 /// A mapping of WebSocket paths to the channels that are open on them.
302304 ws_channels : HashMap < String , HashSet < u32 > > ,
305+ /// A mapping of WebSocket channel IDs to their client socket addresses.
306+ ws_channel_addrs : HashMap < u32 , String > ,
303307 /// The timeout given for `http-server:distro:sys` to respond to a configuration request.
304308 pub timeout : u64 ,
305309}
@@ -451,6 +455,7 @@ impl HttpServer {
451455 http_paths : HashMap :: new ( ) ,
452456 ws_paths : HashMap :: new ( ) ,
453457 ws_channels : HashMap :: new ( ) ,
458+ ws_channel_addrs : HashMap :: new ( ) ,
454459 timeout,
455460 }
456461 }
@@ -983,18 +988,32 @@ impl HttpServer {
983988 }
984989
985990 /// Handle a WebSocket open event from the HTTP server.
986- pub fn handle_websocket_open ( & mut self , path : & str , channel_id : u32 ) {
991+ pub fn handle_websocket_open (
992+ & mut self ,
993+ path : & str ,
994+ channel_id : u32 ,
995+ source_socket_addr : Option < String > ,
996+ ) {
987997 self . ws_channels
988998 . entry ( path. to_string ( ) )
989999 . or_insert ( HashSet :: new ( ) )
9901000 . insert ( channel_id) ;
1001+ if let Some ( addr) = source_socket_addr {
1002+ self . ws_channel_addrs . insert ( channel_id, addr) ;
1003+ }
1004+ }
1005+
1006+ /// Get the socket address for a WebSocket channel.
1007+ pub fn get_ws_channel_addr ( & self , channel_id : u32 ) -> Option < & String > {
1008+ self . ws_channel_addrs . get ( & channel_id)
9911009 }
9921010
9931011 /// Handle a WebSocket close event from the HTTP server.
9941012 pub fn handle_websocket_close ( & mut self , channel_id : u32 ) {
9951013 self . ws_channels . iter_mut ( ) . for_each ( |( _, channels) | {
9961014 channels. remove ( & channel_id) ;
9971015 } ) ;
1016+ self . ws_channel_addrs . remove ( & channel_id) ;
9981017 }
9991018
10001019 pub fn parse_request ( & self , body : & [ u8 ] ) -> Result < HttpServerRequest , HttpServerError > {
@@ -1024,8 +1043,12 @@ impl HttpServer {
10241043 channel_id,
10251044 message_type,
10261045 } => ws_handler ( channel_id, message_type, last_blob ( ) . unwrap_or_default ( ) ) ,
1027- HttpServerRequest :: WebSocketOpen { path, channel_id } => {
1028- self . handle_websocket_open ( & path, channel_id) ;
1046+ HttpServerRequest :: WebSocketOpen {
1047+ path,
1048+ channel_id,
1049+ source_socket_addr,
1050+ } => {
1051+ self . handle_websocket_open ( & path, channel_id, source_socket_addr) ;
10291052 }
10301053 HttpServerRequest :: WebSocketClose ( channel_id) => {
10311054 self . handle_websocket_close ( channel_id) ;
0 commit comments