gpt4 book ai didi

dart - 如何使用shelf_web_socket 监听同一端口上的http 和ws 请求

转载 作者:行者123 更新时间:2023-12-05 01:09:39 25 4
gpt4 key购买 nike

https://pub.dartlang.org/packages/shelf_web_socket显示这个例子

import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_web_socket/shelf_web_socket.dart';

void main() {
var handler = webSocketHandler((webSocket) {
webSocket.listen((message) {
webSocket.add("echo $message");
});
});

shelf_io.serve(handler, 'localhost', 8080).then((server) {
print('Serving at ws://${server.address.host}:${server.port}');
});
}

我想知道如何将它与我的 HTTP 服务器初始化结合起来

import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as sIo;
import 'package:shelf_auth/shelf_auth.dart' as sAuth;
import 'package:shelf_auth/src/authentication.dart' as sAuth2;
import 'package:option/option.dart';
import 'package:shelf_web_socket/shelf_web_socket.dart' as sWs;

...

var authMiddleware = sAuth.authenticate(
[new MyAuthenticator()],
sessionHandler: new sAuth.JwtSessionHandler('bla', 'blub', new UserLookup()),
allowHttp: true,
allowAnonymousAccess: false);

var handler = const shelf.Pipeline()
.addMiddleware(shelf.logRequests())
.addMiddleware(authMiddleware)
.addHandler(_handleHttpRequest);

// var wsHandler = sWs.webSocketHandler(_handleWebSocketConnect);

sIo.serve(handler, '0.0.0.0', servePort).then((server) {
_log.finest('Serving at http://${server.address.host}:${server.port}');
});

需要做什么才能 wsHandler调用 WebSocket 连接和 handler继续处理 HTTP 请求(如果可能在同一端口上),并在可能的情况下使用配置的身份验证和 session 管理。

我在不同的端口上尝试过,但是使用身份验证/ session 中间件(不知道是否应该一起使用)

   var authMiddleware = sAuth.authenticate(
[new MyAuthenticator()],
sessionHandler: new sAuth.JwtSessionHandler('bla', 'blub', new UserLookup()),
allowHttp: true,
allowAnonymousAccess: false);

var handler = const shelf.Pipeline()
.addMiddleware(shelf.logRequests())
.addMiddleware(authMiddleware)
.addHandler(_handleHttpRequest);

sIo.serve(handler, '0.0.0.0', servePort).then((server) {
_log.finest('Serving at http://${server.address.host}:${server.port}');
});


var wsHandler = const shelf.Pipeline()
.addMiddleware(shelf.logRequests())
.addMiddleware(authMiddleware)
.addHandler(sWs.webSocketHandler(_handleWebSocketConnect));

sIo.serve(wsHandler, '0.0.0.0', servePort + 1).then((server) {
_log.finest('Serving at ws://${server.address.host}:${server.port}');
});

并得到

Illegal argument(s): webSocketHandler may only be used with a server that supports request hijacking.

最佳答案

目前您的根处理程序是 http 处理程序。您需要设置一个处理程序,有条件地将请求发送到 ws 处理程序或 http 请求的另一个处理程序。例如

/ws -> 你的 ws 处理程序

/rest -> 你的另一个处理程序

最简单的方法是使用像shelf_route这样的路由器。

然而最近有人尝试了这个并点击了 bug in shelf that stopped this working .正如你在下面提到的那样是固定的,但没有合并。

一旦问题得到解决,你应该能够做到

import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_route/shelf_route.dart' as route;
import 'package:shelf_web_socket/shelf_web_socket.dart' as sWs;
import 'package:shelf_auth/shelf_auth.dart' as sAuth;
import 'dart:async';
import 'package:option/option.dart';
import 'package:shelf_exception_response/exception_response.dart';

void main(List<String> arguments) {

var authMiddleware = sAuth.authenticate(
[new MyAuthenticator()],
sessionHandler: new sAuth.JwtSessionHandler('bla', 'blub', new UserLookup()),
allowHttp: true,
allowAnonymousAccess: false);


var router = (route.router()
..get('/rest', _handleHttpRequest)
..get('/ws', sWs.webSocketHandler(_handleWebSocketConnect)));

var handler = const shelf.Pipeline()
.addMiddleware(exceptionResponse())
.addMiddleware(shelf.logRequests())
.addMiddleware(authMiddleware)
.addHandler(router.handler);

route.printRoutes(router);

io.serve(handler, '127.0.0.1', 8080).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}

在问题解决之前,您可以将 router.handler 替换为

var hackyRouterHandler = (shelf.Request request) {
var path = request.url.path;
if (path.startsWith('/rest')) {
return _handleHttpRequest(request);
}
else if (path.startsWith('/ws')) {
return sWs.webSocketHandler(_handleWebSocketConnect)(request);
}
else {
throw new NotFoundException();
}
};

关于dart - 如何使用shelf_web_socket 监听同一端口上的http 和ws 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26174202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com