-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlocal_server.dart
42 lines (37 loc) · 1.23 KB
/
local_server.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_static/shelf_static.dart';
import 'package:shelf_web_socket/shelf_web_socket.dart';
void main(List<String> args) {
final port = args.isEmpty ? 80 : int.parse(args.first);
final staticHandler =
createStaticHandler('app/build/web', defaultDocument: 'index.html');
final socketHandler = webSocketHandler((webSocket, _) async {
final proxied = await WebSocket.connect('ws://localhost:10357');
webSocket.stream.listen((message) {
proxied.add(message);
}, onDone: () {
proxied.close();
});
proxied.listen((message) {
webSocket.sink.add(message);
}, onDone: () {
webSocket.sink.close();
});
});
// Serve /connect requests with the web socket handler.
// Serve other requests with the static file handler.
handler(Request request) {
print(request.url.path);
if (request.url.path == 'connect') {
return socketHandler(request);
} else {
return staticHandler(request);
}
}
// Serve on every ip
io.serve(handler, InternetAddress.anyIPv4, port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}