我尝试将flutter连接到Express js套接字,但它拒绝了连接或完全没有反应。
我从张量理解了这个示例,但是当我重新加载它时并没有给我任何错误,它告诉我dart:html包丢失了。它与回声 websocket.io 一起工作,但不能与我的后端
一起工作这是乱码
import 'package:web_socket_channel/io.dart';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
WebSocketChannel channel;
@override
void initState() {
super.initState();
channel = IOWebSocketChannel.connect("ws://mighty-peak-62403.herokuapp.com");
}
@override
void dispose() {
// TODO: implement dispose
channel.sink.close();
super.dispose();
}
void _incrementCounter() {
channel.sink.add("hello world");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: StreamBuilder(
stream: channel.stream,
builder: (context, snapShot) =>
snapShot.hasData ? Text(snapShot.data) : Text("doenst work"),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
这是后端
const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const port = process.env.PORT || 5000
app.get('/', function(req, res) {
res.status(200).send("w");
});
io.on('connection', (socket) => {
console.log(`Client SocketID: ${socket.id}.`);
socket.emit('sayHello', {Hello: 'world!'});
socket.on('sayHello', (greeting) => {
console.log(`Hello, ${greeting['Hello']}`);
});
});
server.listen(port,()=>console.log("listening"));
这是从heroku-app上的日志获取的错误
2019-10-24T23:25:39.593874 + 00:00 heroku [router]:at =错误代码= H13 desc =“连接离子关闭,无响应” method = GET path =“ /” host = mighty-peak-62403.herokuap p.com request_id = f9960a12-6f2a-4e6c-9810-fea27d521d51 fwd =“ 156.219.177.165” 强力 = web.1 connect = 0ms服务= 1004ms状态= 503字节= 0协议= http
我希望heroku应用程序能够提供我连接flutter应用程序时实现的日志,但是我却因为错误H13而导致连接断开