使用EventFlux库时无法连接localhost的问题排查
Flutter连接Golang SSE服务出现Connection Refused错误排查方案
我正在用Flutter前端+Golang后端实现Server-Sent Event(SSE),但Flutter端连接后端时出现连接拒绝错误,请求帮忙排查。
错误信息
Error connecting to EventFlux: Instance of 'EventFluxException'
I/flutter ( 9445): Exception details: ClientException with SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 38134, uri=http://localhost:8080/events
Flutter代码
import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:eventflux/client.dart'; import 'package:eventflux/enum.dart'; import 'package:eventflux/eventflux.dart'; import 'package:eventflux/models/data.dart'; import 'package:eventflux/models/response.dart'; import 'package:flutter/material.dart'; import 'package:flutter_client_sse/constants/sse_request_type_enum.dart'; import 'package:flutter_client_sse/flutter_client_sse.dart'; import 'package:sse_stream/sse_stream.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter EventFlux Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final EventFlux _eventFlux = EventFlux.instance; String _receivedData = ""; final dio = Dio(); @override void initState() { super.initState(); _connectToEventFlux(); } void _connectToEventFlux() { _eventFlux.connect( EventFluxConnectionType.get, 'http://localhost:8080/events', onSuccessCallback: (EventFluxResponse? response) { response!.stream?.listen((data) { // Update the UI with the received data setState(() { // Assuming your data is a string, you can update it accordingly // For example, you can store the data in a variable to use in the UI _receivedData = data.toString(); }); }); }, onError: (error) { // Handle errors print('Error connecting to EventFlux: $error'); // Print more details about the exception if available print('Exception details: ${error.message}'); }, autoReconnect: true, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Flutter EventFlux Demo'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ // Display the received data in the UI Text( 'Received Data:', ), Text( _receivedData, style: Theme.of(context).textTheme.headline4, ), ], ), ), ); } @override void dispose() { _eventFlux.disconnect(); super.dispose(); } }
排查步骤及修复方案
修正Flutter连接地址
- Android模拟器:将
http://localhost:8080/events替换为http://10.0.2.2:8080/events,模拟器的localhost指向自身,10.0.2.2是宿主机的专用别名。 - iOS模拟器:可直接用
localhost;真机需替换为电脑的局域网IP(如http://192.168.1.100:8080/events,IP通过ifconfig/ipconfig查询)。
- Android模拟器:将
确认Golang后端状态
- 检查服务是否启动,监听端口是否为8080,且绑定地址为
0.0.0.0(而非localhost,否则仅本机可访问),示例监听代码:http.ListenAndServe("0.0.0.0:8080", nil) - 用浏览器或curl访问
http://localhost:8080/events,验证接口是否能正常建立连接并返回SSE数据。
- 检查服务是否启动,监听端口是否为8080,且绑定地址为
检查端口与防火墙
- 确认8080端口未被占用:Mac/Linux用
lsof -i :8080,Windows用netstat -ano | findstr :8080查询。 - 关闭电脑防火墙或添加规则允许8080端口的网络请求。
- 确认8080端口未被占用:Mac/Linux用
验证后端SSE规范
- 确保Golang返回的响应头包含以下字段:
w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") - 保证接口不会立即关闭连接,持续发送事件数据。
- 确保Golang返回的响应头包含以下字段:
内容的提问来源于stack exchange,提问作者ong ai ling
相关产品推荐
相关产品推荐

