连接中断时,如果缺少pong,Websocket不会关闭

连接中断时,如果缺少pong,Websocket不会关闭,websocket,dart,Websocket,Dart,应使用WebSocketStatus关闭Websockets。如果在pingInterval内未收到pong消息,则将退出。为什么当连接丢失时(例如使用未插入的电缆时),此功能不起作用?这是预期的行为吗?如果是这样,ping消息的意义是什么?(我知道TCP超时的事情。) 复制: 在较低的ping间隔(例如1ms)下,websocket正确关闭,关闭代码==1001 如果ping间隔很长(例如1000ms),在一段时间后关闭连接,websocket将不会关闭(closeCode为null) 下面是

应使用WebSocketStatus关闭Websockets。如果在pingInterval内未收到pong消息,则将退出。为什么当连接丢失时(例如使用未插入的电缆时),此功能不起作用?这是预期的行为吗?如果是这样,ping消息的意义是什么?(我知道TCP超时的事情。)

复制:

  • 在较低的ping间隔(例如1ms)下,websocket正确关闭,关闭代码==1001
  • 如果ping间隔很长(例如1000ms),在一段时间后关闭连接,websocket将不会关闭(closeCode为null)
  • 下面是要复制的代码:

    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
    
    消息:你好,15625979165221
    留言:你好,156279168221
    留言:你好,156279171221
    关闭代码:null
    留言:你好,156279174221
    留言:你好,156279177221
    留言:你好,1562579180221
    关闭代码:null