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

Use web socket along Http request #182

Open
guilherme-diniz opened this issue Mar 9, 2021 · 1 comment
Open

Use web socket along Http request #182

guilherme-diniz opened this issue Mar 9, 2021 · 1 comment

Comments

@guilherme-diniz
Copy link

Hi,

Is there a way to setup WebSocket listeners along with http requests?

@paulocagol
Copy link

import 'dart:async';

import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf_web_socket/shelf_web_socket.dart';

/// ./server.exe
@CloudFunction()
FutureOr<Response> function(Request request) async {
  final app = Router();

  app.get('/', (Request request) {
    return Response.ok('root');
  });

  return app(request);
}

@CloudFunction()
FutureOr<Response> webSocket(Request request) async {
  var app = Router();

  var wsHandle = webSocketHandler((webSocket) {
    webSocket.stream.listen((message) {
      print(message);
      webSocket.sink.add('echo $message');
    });
  });

  app.get('/', wsHandle);

  return app(request);
}
./bin/server.exe --target=webSocket

Connect in localhost:8080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants