Skip to content

Commit

Permalink
Resolve #55 how to custom parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jumperchen committed Feb 14, 2024
1 parent 23b4098 commit fc64882
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/src/darty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Author: jumperchen<[email protected]>

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.
Expand Down Expand Up @@ -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<String, dynamic> _opts;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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


6 changes: 5 additions & 1 deletion test_nodejs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
34 changes: 29 additions & 5 deletions web/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object?> encode(Object? obj) {
print('MyEncoder: $obj');
return super.encode(obj);
}
}
class MyDecoder extends Decoder {
@override
add(obj) {
print('MyDecoder: $obj');
return super.add(obj);
}
}

0 comments on commit fc64882

Please sign in to comment.