swift-websocket

WebSocket 支持

概述

包含 WebSocket 支持的包。它包含三个库:

客户端

WebSocketClient 构建在结构化并发之上。当您连接时,它会调用您提供的闭包,并提供一个入站帧流、一个用于写入出站帧的写入器和一个上下文结构。当您退出闭包时,客户端将自动为您执行关闭握手。

import WSClient

let ws = WebSocketClient.connect(url: "ws://mywebsocket.com/ws") { inbound, outbound, context in
    try await outbound.write(.text("Hello"))
    // you can convert the inbound stream of frames into a stream of full messages using `messages(maxSize:)`
    for try await frame in inbound.messages(maxSize: 1 << 14) {
        context.logger.info(frame)
    }
}