redo types
This commit is contained in:
parent
903c578e75
commit
eb5d03c88b
@ -24,13 +24,6 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
// @objc_block is undocumented, but is used because Swift assumes that all
|
|
||||||
// Objective-C blocks are copied, but Objective-C assumes that Swift will copy it.
|
|
||||||
// And the way things are done here, the bridging fails to copy the block in
|
|
||||||
// SocketAckMap#addAck
|
|
||||||
public typealias AckCallback = @objc_block (NSArray?) -> Void
|
|
||||||
public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void
|
|
||||||
|
|
||||||
struct SocketAckMap {
|
struct SocketAckMap {
|
||||||
private var acks = [Int: AckCallback]()
|
private var acks = [Int: AckCallback]()
|
||||||
private var waiting = [Int: Bool]()
|
private var waiting = [Int: Bool]()
|
||||||
|
|||||||
@ -30,20 +30,10 @@ extension String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray<NSData>?)
|
|
||||||
private typealias ProbeWaitQueue = [Probe]
|
|
||||||
|
|
||||||
public enum PacketType:String {
|
|
||||||
case OPEN = "0"
|
|
||||||
case CLOSE = "1"
|
|
||||||
case PING = "2"
|
|
||||||
case PONG = "3"
|
|
||||||
case MESSAGE = "4"
|
|
||||||
case UPGRADE = "5"
|
|
||||||
case NOOP = "6"
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SocketEngine: NSObject, WebSocketDelegate {
|
public class SocketEngine: NSObject, WebSocketDelegate {
|
||||||
|
private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray<NSData>?)
|
||||||
|
private typealias ProbeWaitQueue = [Probe]
|
||||||
|
|
||||||
private let workQueue = NSOperationQueue()
|
private let workQueue = NSOperationQueue()
|
||||||
private let emitQueue = dispatch_queue_create(
|
private let emitQueue = dispatch_queue_create(
|
||||||
"engineEmitQueue".cStringUsingEncoding(NSUTF8StringEncoding), DISPATCH_QUEUE_SERIAL)
|
"engineEmitQueue".cStringUsingEncoding(NSUTF8StringEncoding), DISPATCH_QUEUE_SERIAL)
|
||||||
@ -84,6 +74,16 @@ public class SocketEngine: NSObject, WebSocketDelegate {
|
|||||||
}
|
}
|
||||||
var ws:WebSocket?
|
var ws:WebSocket?
|
||||||
|
|
||||||
|
public enum PacketType:String {
|
||||||
|
case OPEN = "0"
|
||||||
|
case CLOSE = "1"
|
||||||
|
case PING = "2"
|
||||||
|
case PONG = "3"
|
||||||
|
case MESSAGE = "4"
|
||||||
|
case UPGRADE = "5"
|
||||||
|
case NOOP = "6"
|
||||||
|
}
|
||||||
|
|
||||||
public init(client:SocketEngineClient, forcePolling:Bool,
|
public init(client:SocketEngineClient, forcePolling:Bool,
|
||||||
forceWebsockets:Bool, withCookies cookies:[NSHTTPCookie]?) {
|
forceWebsockets:Bool, withCookies cookies:[NSHTTPCookie]?) {
|
||||||
self.client = client
|
self.client = client
|
||||||
|
|||||||
@ -24,9 +24,6 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void
|
|
||||||
public typealias AckEmitter = (AnyObject...) -> Void
|
|
||||||
|
|
||||||
private func emitAckCallback(socket:SocketIOClient, num:Int)
|
private func emitAckCallback(socket:SocketIOClient, num:Int)
|
||||||
// Curried
|
// Curried
|
||||||
(items:AnyObject...) -> Void {
|
(items:AnyObject...) -> Void {
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
// Socket.IO-Swift
|
// Socket.IO-Swift
|
||||||
//
|
//
|
||||||
// Created by Erik Little on 3/16/15.
|
// Created by Erik Little on 3/16/15.
|
||||||
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
// in the Software without restriction, including without limitation the rights
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
|||||||
@ -306,7 +306,7 @@ public class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let packet = SocketPacket(type: nil, data: args, nsp: self.nsp, id: ack)
|
let packet = SocketPacket(type: nil, data: args, nsp: self.nsp, id: ack)
|
||||||
var str:String
|
let str:String
|
||||||
|
|
||||||
SocketParser.parseForEmit(packet)
|
SocketParser.parseForEmit(packet)
|
||||||
str = packet.createMessageForEvent(event)
|
str = packet.createMessageForEvent(event)
|
||||||
@ -326,7 +326,7 @@ public class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let packet = SocketPacket(type: nil, data: args, nsp: self!.nsp, id: ack)
|
let packet = SocketPacket(type: nil, data: args, nsp: self!.nsp, id: ack)
|
||||||
var str:String
|
let str:String
|
||||||
|
|
||||||
SocketParser.parseForEmit(packet)
|
SocketParser.parseForEmit(packet)
|
||||||
str = packet.createAck()
|
str = packet.createAck()
|
||||||
|
|||||||
@ -24,24 +24,6 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
enum SocketPacketType:Int {
|
|
||||||
case CONNECT = 0
|
|
||||||
case DISCONNECT = 1
|
|
||||||
case EVENT = 2
|
|
||||||
case ACK = 3
|
|
||||||
case ERROR = 4
|
|
||||||
case BINARY_EVENT = 5
|
|
||||||
case BINARY_ACK = 6
|
|
||||||
|
|
||||||
init(str:String) {
|
|
||||||
if let int = str.toInt() {
|
|
||||||
self = SocketPacketType(rawValue: int)!
|
|
||||||
} else {
|
|
||||||
self = SocketPacketType(rawValue: 4)!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SocketPacket {
|
class SocketPacket {
|
||||||
var binary = ContiguousArray<NSData>()
|
var binary = ContiguousArray<NSData>()
|
||||||
var currentPlace = 0
|
var currentPlace = 0
|
||||||
@ -90,7 +72,7 @@ class SocketPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createMessageForEvent(event:String) -> String {
|
func createMessageForEvent(event:String) -> String {
|
||||||
var message:String
|
let message:String
|
||||||
var jsonSendError:NSError?
|
var jsonSendError:NSError?
|
||||||
|
|
||||||
if self.binary.count == 0 {
|
if self.binary.count == 0 {
|
||||||
|
|||||||
52
SwiftIO/SocketTypes.swift
Normal file
52
SwiftIO/SocketTypes.swift
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
//
|
||||||
|
// SocketTypes.swift
|
||||||
|
// SocketIO-Swift
|
||||||
|
//
|
||||||
|
// Created by Erik Little on 4/8/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_block is undocumented, but is used because Swift assumes that all
|
||||||
|
// Objective-C blocks are copied, but Objective-C assumes that Swift will copy it.
|
||||||
|
// And the way things are done here, the bridging fails to copy the block in
|
||||||
|
// SocketAckMap#addAck
|
||||||
|
public typealias AckCallback = @objc_block (NSArray?) -> Void
|
||||||
|
public typealias AckEmitter = (AnyObject...) -> Void
|
||||||
|
public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void
|
||||||
|
public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void
|
||||||
|
|
||||||
|
enum SocketPacketType:Int {
|
||||||
|
case CONNECT = 0
|
||||||
|
case DISCONNECT = 1
|
||||||
|
case EVENT = 2
|
||||||
|
case ACK = 3
|
||||||
|
case ERROR = 4
|
||||||
|
case BINARY_EVENT = 5
|
||||||
|
case BINARY_ACK = 6
|
||||||
|
|
||||||
|
init(str:String) {
|
||||||
|
if let int = str.toInt() {
|
||||||
|
self = SocketPacketType(rawValue: int)!
|
||||||
|
} else {
|
||||||
|
self = SocketPacketType(rawValue: 4)!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user