Skip to content

Commit

Permalink
Merge pull request #25 from bruce3x/master
Browse files Browse the repository at this point in the history
io_websocket support headers
  • Loading branch information
jumperchen authored May 20, 2019
2 parents 7e7494f + d7ba432 commit 3c7582b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/src/engine/socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Socket extends EventEmitter {
Transport transport;
bool supportsBinary;
bool upgrading;
Map extraHeaders;

Socket(String uri, Map opts) {
opts = opts ?? <dynamic, dynamic>{};
Expand Down Expand Up @@ -137,6 +138,7 @@ class Socket extends EventEmitter {
this.perMessageDeflate['threshold'] = 1024;
}

this.extraHeaders = opts['extraHeaders'] ?? {};
// SSL options for Node.js client
// this.pfx = opts.pfx || null;
// this.key = opts.key || null;
Expand Down Expand Up @@ -239,7 +241,7 @@ class Socket extends EventEmitter {
// 'rejectUnauthorized: options.rejectUnauthorized || this.rejectUnauthorized,
'perMessageDeflate':
options['perMessageDeflate'] ?? this.perMessageDeflate,
// 'extraHeaders: options['extraHeaders ?? this.extraHeaders,
'extraHeaders': options['extraHeaders'] ?? this.extraHeaders,
// 'forceNode: options.forceNode || this.forceNode,
// 'localAddress: options.localAddress || this.localAddress,
'requestTimeout': options['requestTimeout'] ?? this.requestTimeout,
Expand Down
6 changes: 4 additions & 2 deletions lib/src/engine/transport/io_websocket_transport.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019 Potix Corporation. All Rights Reserved
// Copyright (C) 2019 Potix Corporation. All Rights Reserved
// History: 2019-01-21 12:13
// Author: jumperchen<jumperchen@potix.com>

Expand All @@ -19,21 +19,23 @@ class IOWebSocketTransport extends Transport {

bool supportsBinary;
Map perMessageDeflate;
Map extraHeaders;
WebSocket ws;

IOWebSocketTransport(Map opts) : super(opts) {
var forceBase64 = (opts != null && opts['forceBase64']);
this.supportsBinary = !forceBase64;
this.perMessageDeflate = opts['perMessageDeflate'];
this.protocols = opts['protocols'];
this.extraHeaders = opts['extraHeaders'];
}

void doOpen() async {
var uri = this.uri();
var protocols = this.protocols;

try {
this.ws = await WebSocket.connect(uri, protocols: protocols);
this.ws = await WebSocket.connect(uri, protocols: protocols, headers: extraHeaders);
} catch (err) {
return this.emit('error', err);
}
Expand Down
7 changes: 5 additions & 2 deletions test/io_client.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (C) 2019 Potix Corporation. All Rights Reserved
// Copyright (C) 2019 Potix Corporation. All Rights Reserved
// History: 2019-01-21 11:56
// Author: jumperchen<jumperchen@potix.com>
import 'dart:async';
import 'package:socket_io_client/socket_io_client.dart' as IO;

main() {
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{'transports': ['websocket']});
IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{
'transports': ['websocket'],
'extraHeaders': {'foo': 'bar'}
});
socket.on('connect', (_) {
print('connect');
socket.emit('msg', 'init');
Expand Down
3 changes: 3 additions & 0 deletions test/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ main() {
// Dart server
var io = new Server();
io.on('connection', (client) {
final headers = client.handshake['headers'];
headers.forEach((k, v) => print('$k => $v'));

print('connection default namespace');
client.on('msg', (data) {
print('data from default => $data');
Expand Down

0 comments on commit 3c7582b

Please sign in to comment.