Skip to content

Commit

Permalink
Make relevant types and members 'public'.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrbrowne committed Oct 15, 2020
1 parent 8fbe4c7 commit 684f364
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
54 changes: 27 additions & 27 deletions Sources/NWWebSocket/Model/NWWebSocket.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import Foundation
import Network

open class NWWebSocket: WebSocketConnection {
public class NWWebSocket: WebSocketConnection {

// MARK: - Public properties

weak var delegate: WebSocketConnectionDelegate?
public weak var delegate: WebSocketConnectionDelegate?

public static var defaultOptions: NWProtocolWebSocket.Options {
let options = NWProtocolWebSocket.Options()
options.autoReplyPing = true

return options
}

// MARK: - Private properties

Expand All @@ -16,29 +23,22 @@ open class NWWebSocket: WebSocketConnection {
private var pingTimer: Timer?
private var intentionalDisconnect: Bool = false

private static var options: NWProtocolWebSocket.Options {
let options = NWProtocolWebSocket.Options()
options.autoReplyPing = true

return options
}

// MARK: - Initialization

convenience init(request: URLRequest,
connectAutomatically: Bool = false,
options: NWProtocolWebSocket.Options = NWWebSocket.options,
connectionQueue: DispatchQueue = .main) {
public convenience init(request: URLRequest,
connectAutomatically: Bool = false,
options: NWProtocolWebSocket.Options = NWWebSocket.defaultOptions,
connectionQueue: DispatchQueue = .main) {

self.init(url: request.url!,
connectAutomatically: connectAutomatically,
connectionQueue: connectionQueue)
}

init(url: URL,
connectAutomatically: Bool = false,
options: NWProtocolWebSocket.Options = NWWebSocket.options,
connectionQueue: DispatchQueue = .main) {
public init(url: URL,
connectAutomatically: Bool = false,
options: NWProtocolWebSocket.Options = NWWebSocket.defaultOptions,
connectionQueue: DispatchQueue = .main) {

endpoint = .url(url)

Expand All @@ -59,7 +59,7 @@ open class NWWebSocket: WebSocketConnection {

// MARK: - WebSocketConnection conformance

func connect() {
public func connect() {
if connection == nil {
connection = NWConnection(to: endpoint, using: parameters)
}
Expand All @@ -69,7 +69,7 @@ open class NWWebSocket: WebSocketConnection {
connection?.start(queue: connectionQueue)
}

func send(string: String) {
public func send(string: String) {
guard let data = string.data(using: .utf8) else {
return
}
Expand All @@ -80,15 +80,15 @@ open class NWWebSocket: WebSocketConnection {
send(data: data, context: context)
}

func send(data: Data) {
public func send(data: Data) {
let metadata = NWProtocolWebSocket.Metadata(opcode: .binary)
let context = NWConnection.ContentContext(identifier: "binaryContext",
metadata: [metadata])

send(data: data, context: context)
}

func listen() {
public func listen() {
connection?.receiveMessage { [weak self] (data, context, _, error) in
guard let self = self else {
return
Expand All @@ -109,7 +109,7 @@ open class NWWebSocket: WebSocketConnection {
}
}

func ping(interval: TimeInterval) {
public func ping(interval: TimeInterval) {
pingTimer = .scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
guard let self = self else {
return
Expand All @@ -120,7 +120,7 @@ open class NWWebSocket: WebSocketConnection {
pingTimer?.tolerance = 0.01
}

func ping() {
public func ping() {
let metadata = NWProtocolWebSocket.Metadata(opcode: .ping)
metadata.setPongHandler(connectionQueue) { [weak self] error in
guard let self = self else {
Expand All @@ -140,7 +140,7 @@ open class NWWebSocket: WebSocketConnection {
send(data: Data(), context: context)
}

func disconnect(closeCode: NWProtocolWebSocket.CloseCode = .protocolCode(.normalClosure)) {
public func disconnect(closeCode: NWProtocolWebSocket.CloseCode = .protocolCode(.normalClosure)) {
intentionalDisconnect = true

// Call `cancel()` directly for a `normalClosure`
Expand Down Expand Up @@ -226,7 +226,7 @@ open class NWWebSocket: WebSocketConnection {

// If a connection closure was sent, inform delegate on completion
if let socketMetadata = context.protocolMetadata.first as? NWProtocolWebSocket.Metadata,
socketMetadata.opcode == .close {
socketMetadata.opcode == .close {
self.delegate?.webSocketDidDisconnect(connection: self,
closeCode: socketMetadata.closeCode,
reason: data)
Expand Down Expand Up @@ -255,8 +255,8 @@ open class NWWebSocket: WebSocketConnection {
/// - Returns: `true` if the error should be reported.
private func shouldReportNWError(_ error: NWError) -> Bool {
if case let .posix(code) = error,
code == .ENOTCONN || code == .ECANCELED,
intentionalDisconnect {
code == .ENOTCONN || code == .ECANCELED,
intentionalDisconnect {
return false
} else {
return true
Expand Down
4 changes: 2 additions & 2 deletions Sources/NWWebSocket/Protocol/WebSocketConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Network

/// Defines a websocket connection.
protocol WebSocketConnection {
public protocol WebSocketConnection {
/// Connect to the websocket.
func connect()

Expand Down Expand Up @@ -32,7 +32,7 @@ protocol WebSocketConnection {
}

/// Defines a delegate for a websocket connection.
protocol WebSocketConnectionDelegate: AnyObject {
public protocol WebSocketConnectionDelegate: AnyObject {
/// Tells the delegate that the WebSocket did connect successfully.
/// - Parameter connection: The active `WebSocketConnection`.
func webSocketDidConnect(connection: WebSocketConnection)
Expand Down

0 comments on commit 684f364

Please sign in to comment.