socket_io_client_new 1.0.0
socket_io_client_new: ^1.0.0 copied to clipboard
A fixed fork of socket_io_client that resolves the port 0 issue. When Dart's Uri.parse() parses URLs without explicit ports, it returns port=0, causing WebSocket connections to fail. This package prop [...]
example/example.dart
// ignore_for_file: avoid_print
import 'package:socket_io_client_new/socket_io_client_new.dart' as IO;
void main() {
// Create a socket connection
// Note: This package fixes the port 0 issue - URLs without explicit ports
// now work correctly (e.g., https://api.example.com instead of
// https://api.example.com:0)
final socket = IO.io(
'https://api.example.com', // No explicit port needed - works correctly!
IO.OptionBuilder()
.setTransports(['websocket', 'polling'])
.enableAutoConnect()
.enableReconnection()
.setReconnectionAttempts(5)
.setReconnectionDelay(1000)
.setTimeout(20000)
.build(),
);
// Connection events
socket.onConnect((_) {
print('Connected to server');
// Emit an event after connecting
socket.emit('message', {'text': 'Hello from Dart!'});
});
socket.onConnectError((error) {
print('Connection error: $error');
});
socket.onDisconnect((_) {
print('Disconnected from server');
});
// Reconnection events
socket.onReconnect((_) {
print('Reconnected to server');
});
socket.onReconnectAttempt((attemptNumber) {
print('Reconnection attempt: $attemptNumber');
});
socket.onReconnectFailed((_) {
print('Reconnection failed');
});
// Listen for custom events
socket.on('response', (data) {
print('Received response: $data');
});
socket.on('broadcast', (data) {
print('Received broadcast: $data');
});
// Emit with acknowledgement
socket.emitWithAck('getData', {'id': 123}, ack: (response) {
print('Server acknowledged with: $response');
});
// Listen to any event (useful for debugging)
socket.onAny((event, data) {
print('Event: $event, Data: $data');
});
}
/// Example of a SocketService class for managing connections
class SocketService {
static SocketService? _instance;
IO.Socket? _socket;
bool _isConnected = false;
bool get isConnected => _isConnected;
factory SocketService() {
_instance ??= SocketService._internal();
return _instance!;
}
SocketService._internal();
void connect(String url, {Map<String, dynamic>? auth}) {
if (_isConnected) return;
final optionBuilder = IO.OptionBuilder()
.setTransports(['websocket', 'polling'])
.enableAutoConnect()
.enableReconnection()
.setReconnectionAttempts(5);
if (auth != null) {
optionBuilder.setAuth(auth);
}
_socket = IO.io(url, optionBuilder.build());
_socket!.onConnect((_) {
_isConnected = true;
print('SocketService: Connected');
});
_socket!.onDisconnect((_) {
_isConnected = false;
print('SocketService: Disconnected');
});
_socket!.onConnectError((error) {
print('SocketService: Connection error - $error');
});
}
void emit(String event, dynamic data) {
_socket?.emit(event, data);
}
void on(String event, Function(dynamic) handler) {
_socket?.on(event, handler);
}
void off(String event) {
_socket?.off(event);
}
void disconnect() {
_socket?.disconnect();
_socket?.dispose();
_socket = null;
_isConnected = false;
}
}