我尝试使用 WebSocket class在 dart:io
包中连接到 SSL WebSocket 服务。这似乎不起作用。目前不支持 wss://
协议(protocol)还是我遗漏了什么。这是使用的代码:
var ws = new WebSocket('wss://...');
ws.onopen = () {
print('open');
};
ws.onmessage = (e) {
print(e.data);
};
我还尝试了替代 API:
var client = new HttpClient(),
conn = new WebSocketClientConnection(client.getUrl(new Uri.fromString('https://...')));
conn.onMessage = (msg) {
print(msg);
};
conn.onOpen = () {
print('open');
};
这似乎也不起作用,我收到如下错误:
1006 HttpParserException: Connection closed before full response header was received 1006 HttpParserException: Invalid request method
我正在使用最新的 SDK。
最佳答案
我假设您使用的是最新版本的 Dart。如果您不是,我建议您进行更新。
您正在调用的一些方法 - conn.onMessage()
、conn.onOpen()
返回 Stream
对象,您需要使用 .listen()
访问(“收听”)流。语法如下:
import 'dart:html';
void main() {
var wss = new WebSocket('wss://echo.websocket.org');
wss.onOpen.listen((item) {
wss.send("hello world");
});
wss.onMessage.listen((message) {
print(message.data);
});
}
等等
关于ssl - 使用 Dart :io WebSocket with wss://protocol (SSL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14496496/