Skip to content

Commit 54b2da8

Browse files
committed
work on redesign
1 parent 5175791 commit 54b2da8

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

SwiftIO/SocketEngine.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension String {
3232

3333
private typealias PollWaitQueue = [() -> Void]
3434

35-
private enum PacketType: String {
35+
public enum PacketType: String {
3636
case OPEN = "0"
3737
case CLOSE = "1"
3838
case PING = "2"
@@ -43,7 +43,7 @@ private enum PacketType: String {
4343
}
4444

4545
public class SocketEngine: NSObject, WebSocketDelegate {
46-
unowned let client:SocketIOClient
46+
unowned let client:SocketEngineClient
4747
private let workQueue = NSOperationQueue()
4848
private let emitQueue = dispatch_queue_create(
4949
"emitQueue".cStringUsingEncoding(NSUTF8StringEncoding), DISPATCH_QUEUE_SERIAL)
@@ -80,7 +80,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
8080
}
8181
var ws:WebSocket?
8282

83-
init(client:SocketIOClient, forcePolling:Bool = false, withCookies cookies:[NSHTTPCookie]?) {
83+
init(client:SocketEngineClient, forcePolling:Bool = false, withCookies cookies:[NSHTTPCookie]?) {
8484
self.client = client
8585
self.forcePolling = forcePolling
8686
self.cookies = cookies
@@ -477,20 +477,14 @@ public class SocketEngine: NSObject, WebSocketDelegate {
477477
}
478478
}
479479

480+
/*
481+
Send a message with type 4
482+
*/
480483
public func send(msg:String, datas:[NSData]? = nil) {
481484
let _send = {[weak self] (msg:String, datas:[NSData]?) -> () -> Void in
482485
return {
483-
if self == nil || !self!.connected {
484-
return
485-
}
486-
487-
if self!.websocket {
488-
// NSLog("sending ws: \(msg):\(datas)")
489-
self?.sendWebSocketMessage(msg, withType: PacketType.MESSAGE, datas: datas)
490-
} else {
491-
// NSLog("sending poll: \(msg):\(datas)")
492-
self?.sendPollMessage(msg, withType: PacketType.MESSAGE, datas: datas)
493-
}
486+
self?.write(msg, withType: PacketType.MESSAGE, withData: datas)
487+
return
494488
}
495489
}
496490

@@ -559,7 +553,8 @@ public class SocketEngine: NSObject, WebSocketDelegate {
559553

560554
self.pingTimer?.invalidate()
561555
dispatch_async(dispatch_get_main_queue()) {
562-
self.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(self.pingInterval!), target: self,
556+
self.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(self.pingInterval!),
557+
target: self,
563558
selector: Selector("sendPing"), userInfo: nil, repeats: true)
564559
}
565560
}
@@ -573,6 +568,22 @@ public class SocketEngine: NSObject, WebSocketDelegate {
573568
}
574569
}
575570

571+
public func write(msg:String, withType type:PacketType, withData data:[NSData]?) {
572+
if !self.connected {
573+
return
574+
}
575+
576+
if self.websocket {
577+
// NSLog("writing ws: \(msg):\(datas)")
578+
self.sendWebSocketMessage(msg, withType: type, datas: data)
579+
} else {
580+
// NSLog("writing poll: \(msg):\(datas)")
581+
self.sendPollMessage(msg, withType: type, datas: data)
582+
}
583+
}
584+
585+
// Delagate methods
586+
576587
public func websocketDidConnect(socket:WebSocket) {
577588
self.websocketConnected = true
578589
self.probing = true

SwiftIO/SocketEngineClient.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// SocketEngineClient.swift
3+
// Socket.IO-Swift
4+
//
5+
// Created by Erik Little on 3/19/15.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
//
25+
26+
import Foundation
27+
28+
@objc public protocol SocketEngineClient {
29+
var ackQueue:dispatch_queue_attr_t! {get}
30+
var handleQueue:dispatch_queue_attr_t! {get}
31+
var emitQueue:dispatch_queue_attr_t! {get}
32+
var reconnecting:Bool {get}
33+
var socketURL:String {get}
34+
var secure:Bool {get}
35+
36+
func parseSocketMessage(msg:String)
37+
func parseBinaryData(data:NSData)
38+
func pollingDidFail(err:NSError?)
39+
func webSocketDidCloseWithCode(code:Int, reason:String, wasClean:Bool)
40+
func webSocketDidFailWithError(error:NSError)
41+
}

SwiftIO/SocketIOClient.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@
2424

2525
import Foundation
2626

27-
public class SocketIOClient: NSObject {
28-
let socketURL:String!
29-
let ackQueue = dispatch_queue_create("ackQueue".cStringUsingEncoding(NSUTF8StringEncoding),
30-
DISPATCH_QUEUE_SERIAL)
31-
let handleQueue = dispatch_queue_create("handleQueue".cStringUsingEncoding(NSUTF8StringEncoding),
32-
DISPATCH_QUEUE_SERIAL)
33-
let emitQueue = dispatch_queue_create("emitQueue".cStringUsingEncoding(NSUTF8StringEncoding),
34-
DISPATCH_QUEUE_SERIAL)
27+
public class SocketIOClient: NSObject, SocketEngineClient {
3528
let reconnectAttempts:Int!
3629
private lazy var params = [String: AnyObject]()
3730
private var ackHandlers = [SocketAckHandler]()
@@ -51,6 +44,13 @@ public class SocketIOClient: NSObject {
5144
internal var currentAck = -1
5245
internal var waitingData = [SocketEvent]()
5346

47+
public let socketURL:String
48+
public let ackQueue = dispatch_queue_create("ackQueue".cStringUsingEncoding(NSUTF8StringEncoding),
49+
DISPATCH_QUEUE_SERIAL)
50+
public let handleQueue = dispatch_queue_create("handleQueue".cStringUsingEncoding(NSUTF8StringEncoding),
51+
DISPATCH_QUEUE_SERIAL)
52+
public let emitQueue = dispatch_queue_create("emitQueue".cStringUsingEncoding(NSUTF8StringEncoding),
53+
DISPATCH_QUEUE_SERIAL)
5454
public var closed:Bool {
5555
return self._closed
5656
}
@@ -379,16 +379,16 @@ public class SocketIOClient: NSObject {
379379
self.connect()
380380
}
381381

382-
func parseSocketMessage(msg:String) {
382+
public func parseSocketMessage(msg:String) {
383383
SocketParser.parseSocketMessage(msg, socket: self)
384384
}
385385

386-
func parseBinaryData(data:NSData) {
386+
public func parseBinaryData(data:NSData) {
387387
SocketParser.parseBinaryData(data, socket: self)
388388
}
389389

390390
// Something happened while polling
391-
func pollingDidFail(err:NSError?) {
391+
public func pollingDidFail(err:NSError?) {
392392
if !self.reconnecting {
393393
self._connected = false
394394
self.handleEvent("reconnect", data: err?.localizedDescription, isInternalMessage: true)
@@ -437,7 +437,7 @@ public class SocketIOClient: NSObject {
437437
}
438438

439439
// Called when the socket is closed
440-
func webSocketDidCloseWithCode(code:Int, reason:String!, wasClean:Bool) {
440+
public func webSocketDidCloseWithCode(code:Int, reason:String, wasClean:Bool) {
441441
self._connected = false
442442
self._connecting = false
443443
if self.closed || !self.reconnects {
@@ -449,7 +449,7 @@ public class SocketIOClient: NSObject {
449449
}
450450

451451
// Called when an error occurs.
452-
func webSocketDidFailWithError(error:NSError!) {
452+
public func webSocketDidFailWithError(error:NSError) {
453453
self._connected = false
454454
self._connecting = false
455455
self.handleEvent("error", data: error.localizedDescription, isInternalMessage: true)

0 commit comments

Comments
 (0)