简体   繁体   English

连接后直接向服务器发送 web 套接字消息

[英]Send web socket message to server directly after connection

The WebSocket server I use needs to receive a message after the connection.我使用的WebSocket服务器连接后需要接收消息。

The only way seems to use WebSocket.connect instead of WebSocketChannel.connect ( https://github.com/dart-lang/web_socket_channel/issues/209 ):唯一的方法似乎是使用WebSocket.connect而不是WebSocketChannel.connecthttps://github.com/dart-lang/web_socket_channel/issues/209 ):

import 'package:web_socket_channel/io.dart';

// ...

IOWebSocketChannel? _channel;

// ...

WebSocket.connect("ws://...").then((ws) {
  this._channel = IOWebSocketChannel(ws);
   _channel!.sink.add(msg);
}

But this is not supported by web.但这不受 web 支持。 How is it possible to do that with both mobile and web support?移动和 web 支持如何做到这一点?

    WebSocket.connect(socketUrl).then((value) {
      _channel = IOWebSocketChannel(value);
      _channel?.stream.listen(onData,
          onError: onError, onDone: onDone, cancelOnError: true);
      debugPrint(
          "webSocket——readyState:${_channel?.innerWebSocket?.readyState}");
      if (_channel?.innerWebSocket?.readyState == 1) {
      } else {}
    }).timeout(const Duration(seconds: 10));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM