简体   繁体   中英

Having trouble sending data to my websocket created in Spring-Boot from Flutter

I am attempting to send data through IOWebSocketChannel in Flutter.io to a WebSocket created in Spring-Boot.

In spring-boot I have created the typical WebSocket config and controllers that are dealing with client's manipulation of my servers WebSocket. I will post them below just for reference.

WebSocketConfiguration.java

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer{

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry){
        registry.addEndpoint("/websocket")
        .setAllowedOrigins("*")     // allow us to connect to ws://localhost:8080/websocket with the default Spring port configuration.
        .withSockJS();   // allows a client which does not support WebSocket natively mimic a WebSocket over an HTTP connection
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry){  //The configureMessageBroker method sets up a simple (in-memory) message broker for our application
       registry.enableSimpleBroker("/topic");                   //topic to be routed back to client
       registry.setApplicationDestinationPrefixes("/app");      //This configuration allows Spring to understand that any message sent to a WebSocket channel name prefixed with /app should be routed to a @MessageMapping in our application.
    }
}

WebSocketController.java

@Controller
public class WebSocketController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketController.class);

    @MessageMapping("/send")
    @SendTo("/topic/messages")
    public Message send(Message message) {
        LOGGER.info(String.format("Received message [%s]", message.toString()));
        LocalDateTime timestamp = LocalDateTime.now();
        return new Message(message.getFrom(), message.getMessage(), timestamp);
    }

}

Now When I try using IOWebSocketChannel I perform the typical protocol of connecting to my configured websocket. Below is the code

final channel = IOWebSocketChannel.connect(
      "ws://10.0.2.2:8080/websocket"
);

I have then created a method that is supposed to send data to my websocket so I attempt to connect to that endpoint which you see is created in WebSocketController.java called app/send/ . Below is the code:

void _sendMessage() {
    IOWebSocketChannel channel = IOWebSocketChannel.connect('ws://10.0.2.2:8080/app/send');
    channel.sink.add(
      json.encode({
        "message": "bars",
      })
    );
  }

Now when I check my Spring-Boot server nothing is logged, however, whenever I hot reload in Flutter Spring Boot and my connection to the websocket times out, tomcat server returns this: Tomcat 服务器通知 websocket 连接

So my question is if anybody has been able to make a breakthrough with sending data through websockets from Flutter into Spring-Boot using IOWebSocketChannel? I am also wondering if anyone has found a way to successfully use a STOMP protocol in Flutter.io? I was using stomp_client as it seemed like it was going to do the trick, however correct if I'm wrong, but flutter was giving me errors saying that there doesn't exist any html files, so I'm assuming that library is only for dart in the web.

Your Spring configuration looks good. But client-side you need some tweaks.

I spent some time to figure this out with the https://pub.dev/packages/stomp package. Use a modified version of the connect function provided here . Make sure to use this custom implementation of the connect function.

Future<StompClient> client = customStomp.connect('ws://10.0.2.2:8080/websocket', ...)

Once connected, according to your configuration, you can then send message on the following destination: /app/send .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM