 import Foundation
 import SwiftWebSocket
 import Wrap
+import Unbox
 
 public typealias SocketState = SwiftWebSocket.WebSocketReadyState
 


	@objc public func webSocketError(error: NSError) {
		webSocketDidError(error)
	}

	@objc public func webSocketMessageText(text: String) {
		webSocketDidReceiveMessage(text)
	}

	@objc public func webSocketMessageData(data: NSData) {

		onSocketOpen?()
	}

	private func webSocketDidReceiveMessage(messageData: NSData) {
		do {
			let message: Message = try Unbox(messageData)

			for channel in channels where channel.topic == message.topic {

				channel.triggerEvent(
					message.event,
					ref: message.ref,
					payload: message.payload
				)
			}

			onSocketMessage?(result: .Success(message))
		} catch let error as NSError {
			webSocketDidError(error)
			onSocketMessage?(result: .Failure(error))
		}
	}
	
	private func webSocketDidClose(code code: Int, reason: String, wasClean: Bool) {
