Vert.x是否支持类似Spring4的基于SockJS的STOMP over WebSocket?
Vert.x Support for STOMP over SockJS (Spring 4-Style)
Hey there! Let me break down what you need to know about Vert.x and STOMP over SockJS, similar to the functionality Spring 4 provides:
Core Answer
Unlike Spring 4, which includes out-of-the-box support for STOMP over SockJS, Vert.x doesn’t offer this feature directly in its official STOMP module. The documentation you referenced only covers direct STOMP over WebSocket connections, which is the primary use case the Vert.x team prioritized.
But You Can Build It Yourself
Vert.x’s modular design makes it possible to cobble this together using existing components. Here’s a high-level approach:
- Set up a SockJS server
First, create an HTTP server and attach Vert.x’s SockJS handler to handle fallback connections (like HTTP streaming or long polling) alongside WebSockets. Example code snippet:HttpServer httpServer = vertx.createHttpServer(); SockJSHandler sockJSHandler = SockJSHandler.create(vertx); // Configure SockJS options if needed (like allowed origins, heartbeat) SockJSOptions options = new SockJSOptions().setHeartbeatInterval(20000); sockJSHandler.setOptions(options); httpServer.requestHandler(sockJSHandler); - Bridge SockJS to Vert.x STOMP server
You’ll need to connect the SockJS sockets to your running STOMP server. Since STOMP expects a stream-based connection (likeNetSocket), you can:- Create a custom adapter that wraps the SockJS
Socketinto aNetSocket-compatible interface, then pass it to the STOMP server’sacceptmethod. - Or use the Vert.x Event Bus as a middleman: forward messages from SockJS clients to the Event Bus, and have a STOMP client relay those messages to the STOMP server (and vice versa).
- Create a custom adapter that wraps the SockJS
- Handle STOMP frame encoding/decoding
Since STOMP is a text-based protocol, you can directly pass raw text messages between the SockJS client and STOMP server once the connection is established. Just make sure to validate frames and handle errors like invalid headers or malformed messages.
Key Considerations
- You’ll have to manually handle edge cases that Spring 4 takes care of automatically, like connection retries, fallback logic for older browsers, and STOMP session management.
- Keep an eye on Vert.x’s release notes—while there’s no official support now, future versions might add this feature based on community demand.
内容的提问来源于stack exchange,提问作者tkec
相关产品推荐
相关产品推荐

