Member-only story
Tiny sample about socket io.
pubspec.yaml
socket_io_client: ^0.9.9
websocket.dart
import 'package:socket_io_client/socket_io_client.dart' as IO;
typedef void OnMessageCallback(String tag, dynamic msg);
typedef void OnCloseCallback(int code, String reason);
typedef void OnOpenCallback();
const EVENT = 'socket-event';
class SimpleWebSocket {
String url;
IO.Socket socket;
OnOpenCallback onOpen;
OnMessageCallback onMessage;
OnCloseCallback onClose;
SimpleWebSocket(this.url);
connect() async {
try {
socket = IO.io(url, <String, dynamic>{
'transports': ['websocket']
});
// Dart client
socket.on('connect', (_) {
print('connected');
onOpen();
});
socket.on(EVENT, (data) {
onMessage(EVENT, data);
});
socket.on('exception', (e) => print('Exception: $e'));
socket.on('connect_error', (e) => print('Connect error: $e'));
socket.on('disconnect', (e) {
print('disconnect');
onClose(0, e);
});
socket.on('fromServer', (_) => print(_));
} catch (e) {
this.onClose(500, e.toString());
}
}
send(event, data) {
if (socket != null) {
socket.emit(event, data);
print('send: $event - $data');
}
}
close() {
if (socket != null) socket.close();
}
}