Created
November 14, 2024 13:58
-
-
Save MarceloRab/9100a31439219fa045f0278a2d4427ef to your computer and use it in GitHub Desktop.
Socket + Flutter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Flutte side | |
import 'package:flutter/material.dart'; | |
//ignore: library_prefixes | |
import 'package:socket_io_client/socket_io_client.dart' as IO; | |
const apiSocketUrl = 'wss://<render-end-point>.com'; | |
const apiSocketUrl = 'http://localhost:8000'; | |
//ππΌ obs: Android and Localhost worked perfectly. | |
class SocketUserClient { | |
late final IO.Socket socket; | |
SocketUserClient() { | |
socketUsersClient(); | |
} | |
void socketUsersClient() { | |
socket = IO.io(apiSocketUrl, <String, dynamic>{ | |
'autoConnect': true, | |
'transports': ['websocket'], | |
}); | |
socket.connect(); | |
//debugPrint('π user_socket_client.dart; socketConnected ID -'); | |
socket.onConnect((_) { | |
debugPrint('π user_socket_client.dart; LIGADO socket CLient DART'); | |
}); | |
socket.on('user update', (data) => debugPrint('π user_socket_client.dart; UPDATE - $data')); | |
socket.on('user removed', (data) => debugPrint('π user_socket_client.dart; data - $data ')); | |
socket | |
.onDisconnect((_) => debugPrint('π user_socket_client.dart; Cliente Socket Desconectado')); | |
socket.onError((err) => debugPrint('π user_socket_client.dart; err - $err')); | |
} | |
void disposeSocket() { | |
socket.disconnect(); | |
socket.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment