0 purchases
socket com
Server Side #
Start server at port 3000 #
final server = await ServerSocket.connect(ip: io.InternetAddress.anyIPv4, port: 3000);
server.onConnection((Socket client) {
handleClient(client);
/// You can listen to event from this perticular client
/// using Socket method on
});
copied to clipboard
Client Side #
Connect to server hosted on localhost at port 3000 #
final Socket client = await ClientSocket.connect(host: "0.0.0.0", port: 3000);
handleServerConnection(client);
copied to clipboard
Add event listener pass on as an event #
final registered = client.on('message', (data) {
//TODO: display message
});
copied to clipboard
Listen to any event from server #
client.listen((data){
//TODO :
})
copied to clipboard
data key , value pair => Map<String,dynamic>
Send data for event on #
STEP 1 : Declare class & extend it with DTO
/// remember to exnted class from DTO for data transmission
/// there we will be transferring Message data over socket
class Message extends DTO {
String username;
String message;
Message({required this.username, required this.message});
factory Message.fromJson(Map<String, dynamic> json) {
return Message(username: json['username'], message: json['message']);
}
@override
Map<String, dynamic> toJson() {
return {"username": username, "message": message};
}
/// in case you wish to encode data differently
/// then remember to decode data correspondingly
@override
String encode() {
return json.encode(toJson());
}
}
copied to clipboard
STEP 2 : Send corresponding message
client.send(
'message',
Message(
username: username,
message: 'Hi i am $username',
),
);
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.