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

Web Sockets #8

Open
carson-katri opened this issue Aug 3, 2019 · 0 comments
Open

Web Sockets #8

carson-katri opened this issue Aug 3, 2019 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@carson-katri
Copy link
Owner

carson-katri commented Aug 3, 2019

Socket

Web socket support is a large task, so it may be a while. Here's an example of what the syntax could look like:

let ws = Socket {
    // Implicit "wss://"
    Url("echo.websocket.org")
    Header.Authorization(.basic(username: "username", password: "pass123"))
    ...
}
.onOpen { ... }
.onClose { reason in ... }
.onData { data in ... }
.onString { string in ... }
.onJson { json in ... }
.onError { err in ... }

ws.send("Hello world")
ws.send(myData)
// Sometime later
ws.close()

onClose would return a SocketCloseEvent, which contains the code (URLSessionWebSocketTask.CloseCode) and the reason. You could pass this into the .close method:

ws.close(SocketCloseEvent(code: .goingAway, reason: "I had a problem!"))
ws.close(code: .goingAway, reason: nil)

This will be built atop URLSessionWebSocketTask, available in iOS 13. Custom frames will not be supported with this implementation.

AnySocket

A Socket with Codable support. It could be used with the onObject callback:

struct Message: Decodable {
    let sender: String
    let body: String
}
AnySocket<Message> {
    Url("messaging.example.com")
}
.onObject { message in ... }

SocketView

SwiftUI compatibility is key. It could look something like this:

var body: some View {
    SocketView(Message.self, Socket { ... }) { messages in
        List(messages) { message in
            Text(message.sender)
                .font(.caption)
            Text(message.body)
        }
    }
}

It gives you an array of the response type. The example above shows Codable support.

@carson-katri carson-katri added the enhancement New feature or request label Aug 3, 2019
@carson-katri carson-katri self-assigned this Aug 3, 2019
@carson-katri carson-katri added this to the v1.3.0 milestone Oct 21, 2019
@carson-katri carson-katri removed this from the v1.3.0 milestone Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant