I’m trying to listen to socket (i run it locally), I’m using web_socket_channel, I followed the instruction from the documentation, but I can’t make it work. It keeps telling me this:
WebSocket error: WebSocketChannelException: HttpException: Connection closed before full header was I/flutter (18089): WebSocket error: WebSocketChannelException: HttpException: Connection closed before full header was received, uri = http://10.2.7.14:3003
I/flutter (18089): WebSocket connection closed
E/flutter (18089): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: WebSocketChannelException: HttpException: Connection closed before full header was received, uri = http://10.2.7.14:3003
*note:
- I’m testing my socket using postman, and it works fine.
- I’m using android emulator, API 35
final String _webSocketUrl = 'ws://10.2.7.14:3003';
void listenSocket() {
// Connect to WebSocket server
_channel = IOWebSocketChannel.connect(
(_webSocketUrl), // Replace with your WebSocket URL
);
setState(() {
_isListening = true;
});
_channel!.stream.listen(
(message) {
print('Message received: $message');
// Handle received messages
},
onDone: () {
setState(() {
_isListening = false;
});
print('WebSocket connection closed');
},
onError: (error) {
setState(() {
_isListening = false;
});
print('WebSocket error: $error');
},
);
}
I’m expecting it to stay and listen. but it seems the connection is closed suddenly,
and why is the Uri = http…. i was using ‘ws:’.
some said try flutter clean, but it didn’t work
addition:
previous code was using
_channel = WebSocketChannel.connect(
Uri.parse(_webSocketUrl), // Replace with your WebSocket URL
);
it gave the same result