Streaming: Send message as UTF8 + token in subprotocols fix #1438

This commit is contained in:
Thomas Ricouard 2023-06-23 07:33:10 +02:00
parent 9e01e6b0f6
commit 831ff08082
2 changed files with 9 additions and 4 deletions

View file

@ -62,8 +62,10 @@ public class StreamWatcher: ObservableObject {
}
private func sendMessage(message: StreamMessage) {
task?.send(.data(try! encoder.encode(message)),
completionHandler: { _ in })
if let encodedMessage = try? encoder.encode(message),
let stringMessage = String(data: encodedMessage, encoding: .utf8) {
task?.send(.string(stringMessage), completionHandler: { _ in })
}
}
private func receiveMessage() {

View file

@ -218,8 +218,11 @@ public final class Client: ObservableObject, Equatable, Identifiable, Hashable {
public func makeWebSocketTask(endpoint: Endpoint, instanceStreamingURL: URL?) -> URLSessionWebSocketTask {
let url = makeURL(scheme: "wss", endpoint: endpoint, forceServer: instanceStreamingURL?.host)
let request = makeURLRequest(url: url, endpoint: endpoint, httpMethod: "GET")
return urlSession.webSocketTask(with: request)
var subprotocols: [String] = []
if let oauthToken = critical.withLock({ $0.oauthToken }) {
subprotocols.append(oauthToken.accessToken)
}
return urlSession.webSocketTask(with: url, protocols: subprotocols)
}
public func mediaUpload<Entity: Decodable>(endpoint: Endpoint,