import 'dart:html'; void main() { wsAPI() .then((_) => print('Great success!')) .catchError((_) => print('Miserable failure')); } Future wsAPI() async { try { final socket = await wsConnect('wss://127.0.0.1:7654'); print('${socket.readyState}'); } catch (e) {print(e);} } Future wsConnect(String url) async { final socket = WebSocket(url); socket.onError.listen((_) => throw Exception('connection error')); socket.onClose.listen((_) => throw Exception('connection close')); return socket; }