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
1
Bluek404 OP Dartium的报错:
Exception: InvalidStateError: Still in CONNECTING state. |
2
Bluek404 OP 好吧解决了
用webSocket.onOpen就行 |