Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io_websocket support headers #25

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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