work on redesign

This commit is contained in:
Erik 2015-03-19 13:22:58 -04:00
parent 5175791513
commit 54b2da8395
3 changed files with 80 additions and 28 deletions

View File

@ -32,7 +32,7 @@ extension String {
private typealias PollWaitQueue = [() -> Void]
private enum PacketType: String {
public enum PacketType: String {
case OPEN = "0"
case CLOSE = "1"
case PING = "2"
@ -43,7 +43,7 @@ private enum PacketType: String {
}
public class SocketEngine: NSObject, WebSocketDelegate {
unowned let client:SocketIOClient
unowned let client:SocketEngineClient
private let workQueue = NSOperationQueue()
private let emitQueue = dispatch_queue_create(
"emitQueue".cStringUsingEncoding(NSUTF8StringEncoding), DISPATCH_QUEUE_SERIAL)
@ -80,7 +80,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
}
var ws:WebSocket?
init(client:SocketIOClient, forcePolling:Bool = false, withCookies cookies:[NSHTTPCookie]?) {
init(client:SocketEngineClient, forcePolling:Bool = false, withCookies cookies:[NSHTTPCookie]?) {
self.client = client
self.forcePolling = forcePolling
self.cookies = cookies
@ -477,21 +477,15 @@ public class SocketEngine: NSObject, WebSocketDelegate {
}
}
/*
Send a message with type 4
*/
public func send(msg:String, datas:[NSData]? = nil) {
let _send = {[weak self] (msg:String, datas:[NSData]?) -> () -> Void in
return {
if self == nil || !self!.connected {
self?.write(msg, withType: PacketType.MESSAGE, withData: datas)
return
}
if self!.websocket {
// NSLog("sending ws: \(msg):\(datas)")
self?.sendWebSocketMessage(msg, withType: PacketType.MESSAGE, datas: datas)
} else {
// NSLog("sending poll: \(msg):\(datas)")
self?.sendPollMessage(msg, withType: PacketType.MESSAGE, datas: datas)
}
}
}
dispatch_async(self.emitQueue) {[weak self] in
@ -559,7 +553,8 @@ public class SocketEngine: NSObject, WebSocketDelegate {
self.pingTimer?.invalidate()
dispatch_async(dispatch_get_main_queue()) {
self.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(self.pingInterval!), target: self,
self.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(self.pingInterval!),
target: self,
selector: Selector("sendPing"), userInfo: nil, repeats: true)
}
}
@ -573,6 +568,22 @@ public class SocketEngine: NSObject, WebSocketDelegate {
}
}
public func write(msg:String, withType type:PacketType, withData data:[NSData]?) {
if !self.connected {
return
}
if self.websocket {
// NSLog("writing ws: \(msg):\(datas)")
self.sendWebSocketMessage(msg, withType: type, datas: data)
} else {
// NSLog("writing poll: \(msg):\(datas)")
self.sendPollMessage(msg, withType: type, datas: data)
}
}
// Delagate methods
public func websocketDidConnect(socket:WebSocket) {
self.websocketConnected = true
self.probing = true

View File

@ -0,0 +1,41 @@
//
// SocketEngineClient.swift
// Socket.IO-Swift
//
// Created by Erik Little on 3/19/15.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
@objc public protocol SocketEngineClient {
var ackQueue:dispatch_queue_attr_t! {get}
var handleQueue:dispatch_queue_attr_t! {get}
var emitQueue:dispatch_queue_attr_t! {get}
var reconnecting:Bool {get}
var socketURL:String {get}
var secure:Bool {get}
func parseSocketMessage(msg:String)
func parseBinaryData(data:NSData)
func pollingDidFail(err:NSError?)
func webSocketDidCloseWithCode(code:Int, reason:String, wasClean:Bool)
func webSocketDidFailWithError(error:NSError)
}

View File

@ -24,14 +24,7 @@
import Foundation
public class SocketIOClient: NSObject {
let socketURL:String!
let ackQueue = dispatch_queue_create("ackQueue".cStringUsingEncoding(NSUTF8StringEncoding),
DISPATCH_QUEUE_SERIAL)
let handleQueue = dispatch_queue_create("handleQueue".cStringUsingEncoding(NSUTF8StringEncoding),
DISPATCH_QUEUE_SERIAL)
let emitQueue = dispatch_queue_create("emitQueue".cStringUsingEncoding(NSUTF8StringEncoding),
DISPATCH_QUEUE_SERIAL)
public class SocketIOClient: NSObject, SocketEngineClient {
let reconnectAttempts:Int!
private lazy var params = [String: AnyObject]()
private var ackHandlers = [SocketAckHandler]()
@ -51,6 +44,13 @@ public class SocketIOClient: NSObject {
internal var currentAck = -1
internal var waitingData = [SocketEvent]()
public let socketURL:String
public let ackQueue = dispatch_queue_create("ackQueue".cStringUsingEncoding(NSUTF8StringEncoding),
DISPATCH_QUEUE_SERIAL)
public let handleQueue = dispatch_queue_create("handleQueue".cStringUsingEncoding(NSUTF8StringEncoding),
DISPATCH_QUEUE_SERIAL)
public let emitQueue = dispatch_queue_create("emitQueue".cStringUsingEncoding(NSUTF8StringEncoding),
DISPATCH_QUEUE_SERIAL)
public var closed:Bool {
return self._closed
}
@ -379,16 +379,16 @@ public class SocketIOClient: NSObject {
self.connect()
}
func parseSocketMessage(msg:String) {
public func parseSocketMessage(msg:String) {
SocketParser.parseSocketMessage(msg, socket: self)
}
func parseBinaryData(data:NSData) {
public func parseBinaryData(data:NSData) {
SocketParser.parseBinaryData(data, socket: self)
}
// Something happened while polling
func pollingDidFail(err:NSError?) {
public func pollingDidFail(err:NSError?) {
if !self.reconnecting {
self._connected = false
self.handleEvent("reconnect", data: err?.localizedDescription, isInternalMessage: true)
@ -437,7 +437,7 @@ public class SocketIOClient: NSObject {
}
// Called when the socket is closed
func webSocketDidCloseWithCode(code:Int, reason:String!, wasClean:Bool) {
public func webSocketDidCloseWithCode(code:Int, reason:String, wasClean:Bool) {
self._connected = false
self._connecting = false
if self.closed || !self.reconnects {
@ -449,7 +449,7 @@ public class SocketIOClient: NSObject {
}
// Called when an error occurs.
func webSocketDidFailWithError(error:NSError!) {
public func webSocketDidFailWithError(error:NSError) {
self._connected = false
self._connecting = false
self.handleEvent("error", data: error.localizedDescription, isInternalMessage: true)