我是 EventLoop 期货和承诺的新手。我的软件栈:
我有东西给工作和寻找关于如何改进它,因为我在文档周围失去了一个小建议.map,.flatMap,.always,等。
这是我在 iOS 应用程序中的 gRPC 数据单例的相关函数:
import Foundation
import NIO
import GRPC
class DataRepository {
static let shared = DataRepository()
// skip ...
func readItem(id: Int64, eventLoop: EventLoop) -> EventLoopFuture<V1_ReadResponse> {
// TODO: Is this the right place?
defer {
try? eventLoop.syncShutdownGracefully()
}
let promise = eventLoop.makePromise(of: V1_ReadResponse.self)
var request = V1_ReadRequest()
request.api = "v1"
request.id = …Run Code Online (Sandbox Code Playgroud) 我正在使用MicroExpress在应用程序中创建 Web 服务器SwiftUI。当我在模拟器中运行应用程序时,我可以连接到服务器,但无法使其在我的真实设备上运行。
MicroExpress(基于SwiftNIO)在 iPhone 上创建一个 Web 服务器,并在本地网络中公开一个 API。
我认为我需要添加一项功能或其他功能,但我找不到网络功能,也找不到允许在Xcode 11under中传入网络连接的设置Signing & Capabilities。
如何使应用程序允许传入网络连接?
编辑
如果有必要,我已经添加了Allow Arbitrary Loads允许。我现在HTTP正在连接。HTTP
我运行一个处理大文件(上传和下载)的网站。目前我正在将网站从 Perfect 过渡到 Vapor。在 Perfect 中,将文件流式传输给用户,然后在文件完成后执行一些操作相对简单。
我试图在 Vapor 中做同样的事情,但我似乎无法弄清楚如何在流完成时设置回调,以及当它完成时,它是否完全被用户下载或有中断。
有谁知道如何做到这一点?这是我尝试过的一些事情。
这是基本结构
func downloadFile(request: Request) -> EventLoopFuture<Response> {
//do some authentication stuff here
let promise = request.eventLoop.makePromise(of: Response.self)
let response = request.fileio.streamFile(at: "somePath")
promise.succeed(response)
let future = promise.futureResult
return future
}
Run Code Online (Sandbox Code Playgroud)
第一次修改
func downloadFile(request: Request) -> EventLoopFuture<Response> {
//do some authentication stuff here
let promise = request.eventLoop.makePromise(of: Response.self)
let response = request.fileio.streamFile(at: "somePath")
promise.succeed(response)
let future = promise.futureResult
future.eventLoop.next().execute {
//run some post-download processing here.
//ideally I would like to know …Run Code Online (Sandbox Code Playgroud) 文档中的示例显示了对 EventLoop的现有引用,用于创建未来。
func getNetworkData(args) -> EventLoopFuture<NetworkResponse> {
let promise = eventLoop.makePromise(of: NetworkResponse.self)
queue.async {
. . . do some work . . .
promise.succeed(response)
. . . if it fails, instead . . .
promise.fail(error)
}
return promise.futureResult
}
Run Code Online (Sandbox Code Playgroud)
就我而言,我无权访问当前的 EventLoop。有没有一种简单的方法可以访问它?
或者,我可以访问 EventLoopGroup,并且可以使用它services.lambda.client.eventLoopGroup.next()来获取 EventLoop。但这会产生额外的开销吗?我应该寻找现有的 EventLoop 吗?
这可能是一堆蠕虫,我会尽力描述这个问题.我们有一个长期运行的数据处理工作.我们的行动数据库会在夜间添加,并处理未完成的操作.处理夜间行动大约需要15分钟.在Vapor 2中,我们使用了大量的原始查询来创建PostgreSQL游标并循环遍历它直到它为空.
目前,我们通过命令行参数运行处理.将来我们希望它作为主服务器的一部分运行,以便在执行处理时检查进度.
func run(using context: CommandContext) throws -> Future<Void> {
let table = "\"RecRegAction\""
let cursorName = "\"action_cursor\""
let chunkSize = 10_000
return context.container.withNewConnection(to: .psql) { connection in
return PostgreSQLDatabase.transactionExecute({ connection -> Future<Int> in
return connection.simpleQuery("DECLARE \(cursorName) CURSOR FOR SELECT * FROM \(table)").map { result in
var totalResults = 0
var finished : Bool = false
while !finished {
let results = try connection.raw("FETCH \(chunkSize) FROM \(cursorName)").all(decoding: RecRegAction.self).wait()
if results.count > 0 {
totalResults += results.count
print(totalResults)
// Obviously …Run Code Online (Sandbox Code Playgroud) 我目前正在使用SwiftNIO和SwiftNIOHTTP2 beta在Swift中使用简单的HTTP2客户端.我的实现看起来像这样:
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let bootstrap = ClientBootstrap(group: group)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer { channel in
channel.pipeline.add(handler: HTTP2Parser(mode: .client)).then {
let multiplexer = HTTP2StreamMultiplexer { (channel, streamID) -> EventLoopFuture<Void> in
return channel.pipeline.add(handler: HTTP2ToHTTP1ClientCodec(streamID: streamID, httpProtocol: .https))
}
return channel.pipeline.add(handler: multiplexer)
}
}
defer {
try! group.syncShutdownGracefully()
}
let url = URL(string: "https://strnmn.me")!
_ = try bootstrap.connect(host: url.host!, port: url.port ?? 443)
.wait()
Run Code Online (Sandbox Code Playgroud)
不幸的是,连接始终失败并出现错误:
nghttp2错误:当我们期望SETTINGS帧时,远程对等体返回了意外数据.也许,peer不能正确支持HTTP/2.
但是,从命令行使用nghttp2连接和发出一个简单的请求可以正常工作.
$ nghttp -vn https://strnmn.me
[ 0.048] Connected
The negotiated protocol: h2 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 SwiftNIO 构建 TCP 服务器。服务器在网络中启动,但客户端不知道 IP 地址。因此,我也想启动一个 UDP 服务器,如果客户端出现,他会向网络发送广播消息。服务器将接收并应答,以便客户端现在知道 IP 地址。
是否可以用 SwiftNIO 构建这样的东西?
我正在尝试在我的iOS应用中创建websocket服务器和客户端,在此示例实现的帮助下,我成功地做到了这一点。(https://github.com/apple/swift-nio/tree/master/Sources/NIOWebSocketServer)-因此当前的工作情况是,我在应用启动时运行websocket服务器,然后将客户端加载到可以连接的webview中对它。
现在我的问题是我希望我的服务器保护Websocket服务器安全(基本上从HTTPS html页面连接到websocket服务器)
我是网络编程的新手,至少可以说Swift-nio文档没有。据我了解,我可以使用(https://github.com/apple/swift-nio-transport-services)
我发现了这个线程正是我所需要的-https://github.com/apple/swift-nio-transport-services/issues/39-只要我不在乎用例,就可以禁用TLS身份验证可以连接websocket。
所以我的问题是如何扩展我的客户端(https://github.com/apple/swift-nio/tree/master/Sources/NIOWebSocketClient)和服务器(https://github.com/apple/swift-nio/ tree / master / Sources / NIOWebSocketServer)以使用swift-nio-transport-service。
我可以添加NIOSSLContext和东西,但我想我需要添加EventLoopGroup和新bootstrap方法。我知道答案就在那...。但是我似乎无法确切指出。
任何指针将不胜感激。
谢谢。
我有一组值映射到多个承诺,每个承诺给我一个 EventLoopFuture。所以我最终得到了一个具有可变大小 [EventLoopFuture] 的方法,我需要所有响应都成功才能继续。如果其中一个或多个返回错误,我需要执行错误场景。
在继续成功路径或错误路径之前,如何等待整个 [EventLoopFuture] 完成?