diff --git a/CHANGELOG.md b/CHANGELOG.md index bf514a6..5e43e5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 3.0.0-beta.0 + +**New Feature:** + +* [#55](https://github.com/rikulo/socket.io-client-dart/issues/55) how to custom parser + + ## 2.0.3+1 Fix dart analyzing issues diff --git a/lib/src/darty.dart b/lib/src/darty.dart index a111598..987754b 100644 --- a/lib/src/darty.dart +++ b/lib/src/darty.dart @@ -3,6 +3,7 @@ // Author: jumperchen import 'package:socket_io_client/socket_io_client.dart'; +import 'package:socket_io_common/socket_io_common.dart'; import 'package:socket_io_common/src/util/event_emitter.dart'; /// Default event listeners for dart way API. @@ -60,6 +61,13 @@ extension DartySocket on Socket { } } +/// Parser options +class ParserOptions { + final Encoder Function() encoder; + final Decoder Function() decoder; + ParserOptions({required this.encoder, required this.decoder}); +} + /// Option Builder to help developer to construct an options map. class OptionBuilder { final Map _opts; @@ -114,6 +122,11 @@ class OptionBuilder { return this; } + /// The parser used to marshall/unmarshall packets for transport. + OptionBuilder setParser(ParserOptions parserOptions) { + _opts['parser'] = parserOptions; + return this; + } OptionBuilder setReconnectionAttempts(num attempts) { _opts['reconnectionAttempts'] = attempts; return this; diff --git a/lib/src/manager.dart b/lib/src/manager.dart index 65d1abd..c77207d 100644 --- a/lib/src/manager.dart +++ b/lib/src/manager.dart @@ -96,8 +96,8 @@ class Manager extends EventEmitter { this.uri = uri; if (options['parser'] != null) { - encoder = options['parser'].Encoder(); - decoder = options['parser'].Decoder(); + encoder = options['parser'].encoder(); + decoder = options['parser'].decoder(); } else { encoder = Encoder(); decoder = Decoder(); diff --git a/pubspec.yaml b/pubspec.yaml index 9ee2f76..a614502 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,8 +17,8 @@ dependencies: dev_dependencies: lints: ^3.0.0 test: ">=1.3.0 <2.0.0" -# build_runner: any -# build_web_compilers: any + build_runner: any + build_web_compilers: any # socket_io: any // support Socket.io v2.* only diff --git a/test_nodejs/server.js b/test_nodejs/server.js index 74be9d7..e1c131b 100644 --- a/test_nodejs/server.js +++ b/test_nodejs/server.js @@ -13,7 +13,11 @@ 'use strict'; const app = require('express')(); const server = require('http').createServer(app); -const io = require('socket.io')(server); +const io = require('socket.io')(server, { + cors: { + origin: '*', + } +}); io.on('connection', userSocket => { console.log('connected'); userSocket.on('toServer', data => { diff --git a/web/main.dart b/web/main.dart index 1455c30..63f65cb 100644 --- a/web/main.dart +++ b/web/main.dart @@ -12,18 +12,42 @@ import 'dart:async'; * Copyright (C) 2017 Potix Corporation. All Rights Reserved. */ import 'package:socket_io_client/socket_io_client.dart' as io; +import 'package:socket_io_common/socket_io_common.dart'; void main() { - var socket = io.io('http://localhost:3000'); - socket.on('connect', (_) { - print('connect'); - socket.emit('msg', 'init'); + var socket = io.io( + 'http://localhost:3000', + io.OptionBuilder().setTransports(['polling']) + .setParser(io.ParserOptions(encoder: () => MyEncoder(), decoder: () => MyDecoder())) + // .disableAutoConnect() + .build()); + + // socket.connect(); + + socket.onConnect((_) { + socket.emit('toServer', 'init'); + var count = 0; Timer.periodic(const Duration(seconds: 1), (Timer countDownTimer) { - socket.emit('msg', count++); + socket.emit('toServer', count++); }); }); + socket.on('event', (data) => print(data)); socket.on('disconnect', (_) => print('disconnect')); socket.on('fromServer', (_) => print(_)); } +class MyEncoder extends Encoder { + @override + List encode(Object? obj) { + print('MyEncoder: $obj'); + return super.encode(obj); + } +} +class MyDecoder extends Decoder { + @override + add(obj) { + print('MyDecoder: $obj'); + return super.add(obj); + } +} \ No newline at end of file