连接中断时,缺少套接字时Websocket不会关闭

时间:2019-07-08 09:56:35

标签: websocket dart

如果在pingInterval内未收到pong消息,则应该使用WebSocketStatus.GOING_AWAY关闭Websocket。当失去连接时,例如,为什么这不起作用拔掉电缆?这是预期的行为吗?如果是这样,ping消息的意义何在? (我知道TCP超时的内容。)

复制:

  1. pingInterval较低(例如1ms)时,websocket已通过closeCode == 1001正确关闭
  2. 具有较高的pingInterval(例如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

0 个答案:

没有答案