Last active
May 25, 2021 22:34
-
-
Save IsmailAlamKhan/961ab7beeca82725013b69fd0a2a995e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
pubspec | |
socket_io_client: ^1.0.1 | |
import 'package:flutter/foundation.dart'; | |
import 'package:socket_io_client/socket_io_client.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp(home: Home()); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({ | |
Key key, | |
}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
Socket socket; | |
@override | |
void initState() { | |
super.initState(); | |
socket = io( | |
'http://localhost:3000', | |
OptionBuilder().disableAutoConnect().build(), | |
); | |
socket.onConnect((data) { | |
socket.emit('msg', 'test1'); | |
print('Connected'); | |
setState(() {}); | |
}); | |
socket.onConnectError((data) => print('error $data')); | |
socket.onConnectTimeout((data) => print('onConnectTimeout $data')); | |
socket.onDisconnect((_) { | |
print('Disconnected'); | |
setState(() {}); | |
}); | |
socket.onConnecting((data) => print('Connecting')); | |
} | |
void connect() => socket.connect(); | |
void disconnect() => socket.disconnect(); | |
void _connectDisconect() { | |
if (socket.disconnected) { | |
print('Connecting'); | |
connect(); | |
} else { | |
print('Disconnecting'); | |
disconnect(); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Material App Bar'), | |
), | |
body: Center( | |
child: ElevatedButton( | |
onPressed: _connectDisconect, | |
child: Padding( | |
padding: EdgeInsets.all(10), | |
child: socket.connected ? Text('Disconnect') : Text('Connect'), | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains hidden or 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
pubspec | |
socket_io: ^1.0.0 | |
import 'package:socket_io/socket_io.dart'; | |
void main() { | |
var io = Server(); | |
var nsp = io.of('/some'); | |
nsp.on('connection', (client) { | |
print('connection /some'); | |
client.on('msg', (data) { | |
print('data from /some => $data'); | |
client.emit('fromServer', "ok 2"); | |
}); | |
}); | |
io.on('connection', (client) { | |
print('connection default namespace'); | |
client.on('msg', (data) { | |
print('data from default => $data'); | |
client.emit('fromServer', "ok"); | |
}); | |
}); | |
io.listen(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment