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

Added ability to choose address and provide security context to start HTTPS server #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 9 additions & 10 deletions lib/src/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Server {
Engine? engine;
Encoder encoder = Encoder();
Future<bool>? _ready;
String? address;

/// Server is ready
///
Expand All @@ -65,7 +66,7 @@ class Server {
/// @param {http.Server|Number|Object} http server, port or options
/// @param {Object} options
/// @api public
Server({server, Map? options}) {
Server({server, Map? options, this.address}) {
options ??= {};
path(options.containsKey('path') ? options['path'] : '/socket.io');
serveClient(false != options['serveClient']);
Expand Down Expand Up @@ -244,15 +245,13 @@ class Server {
_logger.fine('creating http server and binding to $srv');
var port = srv.toInt();
var server = StreamServer();
await server.start(port: port);
// HttpServer.bind(InternetAddress.ANY_IP_V4, port).then((
// HttpServer server) {
// this.httpServer = server;
//// server.listen((HttpRequest request) {
//// HttpResponse response = request.response;
//// response.statusCode = HttpStatus.NOT_FOUND;
//// response.close();
//// });
if(opts.containsKey('securityContext')){
SecurityContext securityContext = opts['securityContext'];
await server.startSecure(securityContext,port: port, address: address,);
}
else{
await server.start(port: port, address: address,);
}

var completer = Completer();
var connectPacket = {'type': CONNECT, 'nsp': '/'};
Expand Down
41 changes: 41 additions & 0 deletions test/socket.test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/// 16/02/2017, Created by jumperchen
///
/// Copyright (C) 2017 Potix Corporation. All Rights Reserved.
import 'dart:io';

import 'package:test/test.dart';

import 'package:socket_io/socket_io.dart';
Expand All @@ -33,5 +35,44 @@ void main() {
});
await io.listen(3000);
});
test('Start standalone server on specific address', () async {
var io = Server(address: '0.0.0.0');
var nsp = io.of('/some');
nsp.on('connection', (client) {
print('connection /some');
client.on('msg', (data) {
print('data from /some => $data');
client.emit('fromServer', 'ok 2');
});
});
io.on('connection', (client) {
print('connection default namespace');
client.on('msg', (data) {
print('data from default => $data');
client.emit('fromServer', 'ok');
});
});
await io.listen(4000);
await io.close();
});
test('Start standalone HTTPS server', () async {
var io = Server();
var nsp = io.of('/some');
nsp.on('connection', (client) {
print('connection /some');
client.on('msg', (data) {
print('data from /some => $data');
client.emit('fromServer', 'ok 2');
});
});
io.on('connection', (client) {
print('connection default namespace');
client.on('msg', (data) {
print('data from default => $data');
client.emit('fromServer', 'ok');
});
});
await io.listen(3400,{'securityContext' : SecurityContext.defaultContext});
});
});
}