Reputation: 351
I tried to use the WebSocket class in the dart:io
package to connect to an SSL WebSocket service. This doesn't seem to work. Isn't the wss://
protocol supported at the moment or am I missing something. Here is the code used:
var ws = new WebSocket('wss://...');
ws.onopen = () {
print('open');
};
ws.onmessage = (e) {
print(e.data);
};
I also tried the alternative API:
var client = new HttpClient(),
conn = new WebSocketClientConnection(client.getUrl(new Uri.fromString('https://...')));
conn.onMessage = (msg) {
print(msg);
};
conn.onOpen = () {
print('open');
};
This doesn't seem to work either, I get errors like:
1006 HttpParserException: Connection closed before full response header was received 1006 HttpParserException: Invalid request method
I am using the latest SDK.
Upvotes: 4
Views: 4484
Reputation: 351
Actually, the second alternative works in the latest SDK. The problem was that I was connecting to a Socket.io service and I wasn't performing the HTTP handshake.
Upvotes: 0
Reputation: 14171
I am assuming that you are the latest version of Dart. I would recommend updating if you are not.
Some of the methods you are calling - conn.onMessage()
, conn.onOpen()
return Stream
objects and you need to use a .listen()
to access ('listen to') the stream. Here is the syntax:
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);
});
}
etc.
Can you try that? For more details, read about Stream and Websocket in the api docs.
Upvotes: 2