|
| 1 | +# socket.io-dart |
| 2 | + |
| 3 | +Port of awesome JavaScript Node.js library - [Socket.io v2.0.1](https://github.com/socketio/socket.io) - in Dart |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```dart |
| 8 | +import 'package:socket_io/socket_io.dart'; |
| 9 | +
|
| 10 | +main() { |
| 11 | + var io = new Server(); |
| 12 | + var nsp = io.of('/some'); |
| 13 | + nsp.on('connection', (client) { |
| 14 | + print('connection /some'); |
| 15 | + client.on('msg', (data) { |
| 16 | + print('data from /some => $data'); |
| 17 | + client.emit('fromServer', "ok 2"); |
| 18 | + }); |
| 19 | + }); |
| 20 | + io.on('connection', (client) { |
| 21 | + print('connection default namespace'); |
| 22 | + client.on('msg', (data) { |
| 23 | + print('data from default => $data'); |
| 24 | + client.emit('fromServer', "ok"); |
| 25 | + }); |
| 26 | + }); |
| 27 | + io.listen(3000); |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +```js |
| 32 | +// JS client |
| 33 | +var socket = io('http://localhost:3000'); |
| 34 | +socket.on('connect', function(){console.log('connect')}); |
| 35 | +socket.on('event', function(data){console.log(data)}); |
| 36 | +socket.on('disconnect', function(){console.log('disconnect')}); |
| 37 | +socket.on('fromServer', function(e){console.log(e)}); |
| 38 | +``` |
| 39 | + |
| 40 | +```dart |
| 41 | +// Dart client |
| 42 | +import 'package:socket_io_client/socket_io_client.dart' as IO; |
| 43 | +
|
| 44 | +IO.Socket socket = IO.io('http://localhost:3000'); |
| 45 | +socket.on('connect', (_) { |
| 46 | + print('connect'); |
| 47 | + socket.emit('msg', 'test'); |
| 48 | +}); |
| 49 | +socket.on('event', (data) => print(data)); |
| 50 | +socket.on('disconnect', (_) => print('disconnect')); |
| 51 | +socket.on('fromServer', (_) => print(_)); |
| 52 | +``` |
| 53 | + |
| 54 | +## Multiplexing support |
| 55 | + |
| 56 | +Same as Socket.IO, this project allows you to create several Namespaces, which will act as separate communication channels but will share the same underlying connection. |
| 57 | + |
| 58 | +## Room support |
| 59 | + |
| 60 | +Within each Namespace, you can define arbitrary channels, called Rooms, that sockets can join and leave. You can then broadcast to any given room, reaching every socket that has joined it. |
| 61 | + |
| 62 | +## Transports support |
| 63 | + Refers to [engine.io](https://github.com/socketio/engine.io) |
| 64 | + |
| 65 | +- `polling`: XHR / JSONP polling transport. |
| 66 | +- `websocket`: WebSocket transport. |
| 67 | + |
| 68 | +## Adapters support |
| 69 | + |
| 70 | +* Default socket.io in-memory adapter class. Refers to [socket.io-adapter](https://github.com/socketio/socket.io-adapter) |
0 commit comments