复制:
以下是要复制的代码:
import 'dart:async';
import 'package:web_socket_channel/io.dart';
void main() {
final channel = IOWebSocketChannel.connect("ws://echo.websocket.org/",
pingInterval: Duration(milliseconds: 1000));
channel.stream.listen(print, onError: print, onDone: () => print('done'));
Timer.periodic(Duration(seconds: 3), (t) {
int time = DateTime.now().millisecondsSinceEpoch;
channel.sink.add("message: hi, $time");
});
Timer.periodic(Duration(seconds: 10), (t) {
print("closeCode: ${channel.closeCode}");
});
}
控制台输出:
message: hi, 1562579165221
message: hi, 1562579168221
message: hi, 1562579171221
closeCode: null
message: hi, 1562579174221
message: hi, 1562579177221
message: hi, 1562579180221
closeCode: null <---- cable pulled
closeCode: null
closeCode: null