Created
January 21, 2018 22:00
-
-
Save gonzalonunez/ff23d36bef799a6a4d82d9c79d06771e to your computer and use it in GitHub Desktop.
CarrotSocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Carrot | |
import SocketRocket | |
public class CarrotSocket: NSObject, Socket { | |
// MARK: Lifecycle | |
public init(webSocket: SRWebSocket) { | |
socket = webSocket | |
super.init() | |
socket.delegate = self | |
} | |
// MARK: Socket | |
public weak var eventDelegate: SocketDelegate? | |
public func open() { | |
socket.open() | |
} | |
public func close() { | |
socket.close() | |
} | |
public func send(data: Data) throws { | |
socket.send(data) | |
} | |
// MARK: Private | |
private let socket: SRWebSocket | |
} | |
extension CarrotSocket: SRWebSocketDelegate { | |
public func webSocketDidOpen(_ webSocket: SRWebSocket!) { | |
eventDelegate?.socketDidOpen() | |
} | |
public func webSocket(_ webSocket: SRWebSocket!, didCloseWithCode code: Int, reason: String!, wasClean: Bool) { | |
eventDelegate?.socketDidClose(with: code, reason: reason, wasClean: wasClean) | |
} | |
public func webSocket(_ webSocket: SRWebSocket!, didFailWithError error: Error!) { | |
eventDelegate?.socketDidFail(with: error) | |
} | |
public func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) { | |
switch message { | |
case let data as Data: | |
eventDelegate?.socketDidReceive(data: data) | |
case let message as String: | |
eventDelegate?.socketDidReceive(data: Data(message.utf8)) | |
default: | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment