@@ -28,6 +28,8 @@ mod middlewares;
2828
2929mod injectable;
3030
31+ mod websocket;
32+
3133/// Defines a handler for HTTP GET requests.
3234/// This macro should be used inside an `impl` block of a struct annotated with the `#[controller]` macro.
3335///
@@ -643,3 +645,146 @@ pub fn main(_args: TokenStream, item: TokenStream) -> TokenStream {
643645
644646 output. into ( )
645647}
648+
649+ /// Marks a struct as a WebSocket gateway controller.
650+ /// This macro should be used in combination with the `#[web_socket]` macro for handler implementation.
651+ ///
652+ /// ### Usage
653+ /// ```rust,ignore
654+ /// #[web_socket_gateway]
655+ /// struct ChatSocket;
656+ ///
657+ /// #[web_socket("/chat")]
658+ /// impl ChatSocket {
659+ /// #[on_connection]
660+ /// async fn on_connect(&self, socket: SocketRef) {
661+ /// println!("Client connected");
662+ /// }
663+ /// }
664+ /// ```
665+ #[ proc_macro_attribute]
666+ pub fn web_socket_gateway ( attr : TokenStream , item : TokenStream ) -> TokenStream {
667+ websocket:: expand_websocket_gateway ( attr, item)
668+ }
669+
670+ /// Defines WebSocket handlers for a struct.
671+ /// This macro should be used inside an `impl` block of a struct annotated with the `#[web_socket_gateway]` macro.
672+ ///
673+ /// ### Parameters
674+ /// - `path`: The path for the WebSocket endpoint, e.g., `"/socket"`
675+ ///
676+ /// ### Usage
677+ /// ```rust,ignore
678+ /// #[web_socket_gateway]
679+ /// struct SocketController;
680+ ///
681+ /// #[web_socket("/socket")]
682+ /// impl SocketController {
683+ /// #[on_connection]
684+ /// async fn on_connect(&self, socket: SocketRef) {
685+ /// println!("Client connected");
686+ /// }
687+ ///
688+ /// #[subscribe_message("message")]
689+ /// async fn on_message(&self, socket: SocketRef, Data(msg): Data<String>) {
690+ /// println!("Received: {}", msg);
691+ /// }
692+ ///
693+ /// #[on_disconnect]
694+ /// async fn on_disconnect(&self, socket: WebSocket) {
695+ /// println!("Client disconnected");
696+ /// }
697+ /// }
698+ /// ```
699+ #[ proc_macro_attribute]
700+ pub fn web_socket ( attr : TokenStream , item : TokenStream ) -> TokenStream {
701+ websocket:: expand_websocket ( attr, item)
702+ }
703+
704+ /// Marks a method as a WebSocket connection handler.
705+ /// This method will be called when a client establishes a WebSocket connection.
706+ ///
707+ /// ### Parameters
708+ /// The handler receives a `SocketRef` parameter for interacting with the connected client.
709+ ///
710+ /// ### Usage
711+ /// ```rust,ignore
712+ /// #[on_connection]
713+ /// async fn on_connect(&self, socket: SocketRef) {
714+ /// println!("Client connected: {}", socket.id);
715+ /// }
716+ /// ```
717+ #[ proc_macro_attribute]
718+ pub fn on_connection ( attr : TokenStream , item : TokenStream ) -> TokenStream {
719+ let _ = attr;
720+ item
721+ }
722+
723+ /// Marks a method as a WebSocket disconnection handler.
724+ /// This method will be called when a client disconnects from the WebSocket.
725+ ///
726+ /// ### Parameters
727+ /// The handler receives a `WebSocket` parameter with the disconnected client's information.
728+ ///
729+ /// ### Usage
730+ /// ```rust,ignore
731+ /// #[on_disconnect]
732+ /// async fn on_disconnect(&self, socket: WebSocket) {
733+ /// println!("Client disconnected: {}", socket.id());
734+ /// }
735+ /// ```
736+ #[ proc_macro_attribute]
737+ pub fn on_disconnect ( attr : TokenStream , item : TokenStream ) -> TokenStream {
738+ let _ = attr;
739+ item
740+ }
741+
742+ /// Marks a method as a WebSocket message handler.
743+ /// This method will be called when the client emits an event with the specified message type.
744+ ///
745+ /// ### Parameters
746+ /// - `message_type`: The name of the event to handle, e.g., `"message"` or `"*"` for any event
747+ ///
748+ /// ### Parameters in handler
749+ /// - `socket: SocketRef` - The connected client's socket
750+ /// - `Data(data): Data<T>` - The message payload deserialized to type T
751+ /// - `ack: AckSender` (optional) - For sending acknowledgments back to the client
752+ ///
753+ /// ### Usage
754+ /// ```rust,ignore
755+ /// #[subscribe_message("message")]
756+ /// async fn on_message(&self, socket: SocketRef, Data(msg): Data<String>) {
757+ /// println!("Received: {}", msg);
758+ /// }
759+ ///
760+ /// #[subscribe_message("request")]
761+ /// async fn on_request(&self, Data(req): Data<Request>, ack: AckSender) {
762+ /// ack.send("response").ok();
763+ /// }
764+ /// ```
765+ #[ proc_macro_attribute]
766+ pub fn subscribe_message ( attr : TokenStream , item : TokenStream ) -> TokenStream {
767+ let _ = attr;
768+ item
769+ }
770+
771+ /// Marks a method as a WebSocket fallback handler.
772+ /// This method will be called for any event that doesn't match a specific `#[subscribe_message]` handler.
773+ /// It's useful for debugging or handling dynamic events.
774+ ///
775+ /// ### Parameters in handler
776+ /// - `Event(name): Event` - The event name
777+ /// - `Data(data): Data<T>` - The message payload
778+ ///
779+ /// ### Usage
780+ /// ```rust,ignore
781+ /// #[on_fallback]
782+ /// async fn on_fallback(&self, Event(event): Event, Data(data): Data<Value>) {
783+ /// println!("Unhandled event: {} with data: {:?}", event, data);
784+ /// }
785+ /// ```
786+ #[ proc_macro_attribute]
787+ pub fn on_fallback ( attr : TokenStream , item : TokenStream ) -> TokenStream {
788+ let _ = attr;
789+ item
790+ }
0 commit comments