带有Spring-boot后端的Flutter WebSocket [英] Flutter websocket with Spring-boot backend

查看:190
本文介绍了带有Spring-boot后端的Flutter WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,Flutter在食谱中有WebSocket食谱(此处).这对websocket.org测试服务器非常有用.

Alright, Flutter has the WebSocket recipe in the cookbook (here). And that works great against the websocket.org test server.

问题是我想与自己的WebSocket服务器连接.因此,我首先使用了SpringBoot中的本教程.

The thing is I want to connect with my own WebSocket server. So I first used this tutorial from SpringBoot.

尝试从应用程序(我在这里使用仿真器)向Spring Boot后端发出请求是无效的.然后,我开始修补并从春季启动后端中删除了STOMP,然后将其留给了一个简单的WebSocket传递字符串.使用邮递员甚至是网页时都可以使用,但无法在应用程序中使用

Trying to make a request from the app (I am using the emulator here) to the spring boot backend did not work. I then started tinkering and removed STOMP from the spring boot backend and left it with a simple WebSocket passing strings. It works when using postman or even a webpage but it doesn't work from the app

当前状态在此GitHub上存在(春季启动和Flutter项目): https://github.com/Flavsditz/websocket_sandbox

The current state is present at this GitHub (both the spring boot and flutter projects): https://github.com/Flavsditz/websocket_sandbox

有人在这里有什么提示吗?

Does anyone have any tips here?

我很感激!

推荐答案

经过一番考虑,我发现了问题:

After a bit of consideration, I found the issue:

问题是我的spring-boot服务器在 localhost 上,但是flutter(也是android模拟器)具有自己的环回服务.因此,在Flutter程序中调用 localhost 是指另一个地方,而不是我想要的地方.

The problem is that my spring-boot server was on localhost, but the flutter (which is also the android emulator) has its own loopback service. So calling localhost inside the Flutter program is referring to another place instead of the one I wanted.

我用 localhost 代替了ip 10.0.2.2 ,它是为帮助开发而设置的主机PC的别名.

I've substituted the localhost for the ip 10.0.2.2 which is an alias to the host PC which is set up to help in development.

有关更多信息,请查看以下答案:此处

For more infos check this answer: here

当然,如果您要在真实的设备上进行测试,而不是在外部发布后端,那么此答案可能会更好:

Of course if you wanted to test from a real device than one would need to publish the backend for the outside, so this answer might be better: here

这篇关于带有Spring-boot后端的Flutter WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆