Bluek404
V2EX  ›  问与答

Dart 的 WebSocket 死活发送不了信息

  •  
  •   Bluek404 · Nov 4, 2014 · 2926 views
    This topic created in 4408 days ago, the information mentioned may be changed or developed.
    import 'dart:html';
    
    void main() {
      var webSocket = new WebSocket('ws://127.0.0.1:4001');
    
      var data = "test message";
    
      if (webSocket != null && webSocket.readyState == WebSocket.OPEN) {
        webSocket.send(data);
      } else {
        print('WebSocket not connected, message $data not sent, State: ${webSocket.readyState}');
      }
    }
    

    死活send不了

    状态一直是WebSocket.CONNECTING

    如果直接send的话就报错

    InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

    但是完全可以接收到服务端发送的信息

    完全是按照官方doc写的啊

    官方文档:

    https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-dom-html.WebSocket

    2 replies  •  2014-11-04 14:14:03 +08:00
    Bluek404
    Bluek404
    OP
    Nov 4, 2014
    1
    Dartium的报错:
    Exception: InvalidStateError: Still in CONNECTING state.
    Bluek404
    Bluek404
    OP
    Nov 4, 2014
    2
    好吧解决了
    用webSocket.onOpen就行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1752 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 00:07 · PVG 08:07 · LAX 16:07 · JFK 19:07
    ♥ Do have faith in what you're doing.