Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2453f33191 | ||
|
|
bb3d7fbded | ||
|
|
f54153f3d4 | ||
|
|
709ecc30b3 | ||
|
|
72dd2ef364 | ||
|
|
9ec521a6de | ||
|
|
69c67a2b81 | ||
|
|
dd068cb480 | ||
|
|
3abb26565b | ||
|
|
d7d1da704f | ||
|
|
5a5dc70203 |
No files matched your search
@@ -27,12 +27,18 @@ socket.connect()
|
||||
```objective-c
|
||||
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil];
|
||||
|
||||
[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) {
|
||||
NSLog(@"connected");
|
||||
[socket emit:@"echo" withItems:@[@"echo test"]];
|
||||
[socket emitWithAck:@"ackack" withItems:@[@1]](10, ^(NSArray* data) {
|
||||
NSLog(@"Got ack");
|
||||
[socket on:@"connect" callback:^(NSArray* data, void (^ack)(NSArray*)) {
|
||||
NSLog(@"socket connected");
|
||||
}];
|
||||
|
||||
[socket on:@"currentAmount" callback:^(NSArray* data, void (^ack)(NSArray*)) {
|
||||
double cur = [[data objectAtIndex:0] floatValue];
|
||||
|
||||
[socket emitWithAck:@"canUpdate" withItems:@[@(cur)]](0, ^(NSArray* data) {
|
||||
[socket emit:@"update" withItems:@[@{@"amount": @(cur + 2.50)}]];
|
||||
});
|
||||
|
||||
ack(@[@"Got your currentAmount, ", @"dude"]);
|
||||
}];
|
||||
|
||||
[socket connect];
|
||||
@@ -88,12 +94,13 @@ Constructors
|
||||
|
||||
Options
|
||||
-------
|
||||
- `connectParams: [String: AnyObject]?` - Dictionary whose contents will be passed with the connection.
|
||||
- `reconnects: Bool` Default is `true`
|
||||
- `reconnectAttempts: Int` Default is `-1` (infinite tries)
|
||||
- `reconnectWait: Int` Default is `10`
|
||||
- `forcePolling: Bool` Default is `false`. `true` forces the client to use xhr-polling.
|
||||
- `forceWebsockets: Bool` Default is `false`. `true` forces the client to use WebSockets.
|
||||
- `nsp: String` Default is `"/"`
|
||||
- `nsp: String` Default is `"/"`. Connects to a namespace.
|
||||
- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil.
|
||||
- `log: Bool` If `true` socket will log debug messages. Default is false.
|
||||
- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.
|
||||
@@ -108,9 +115,11 @@ Methods
|
||||
5. `emitWithAck(event:String, _ items:AnyObject...) -> (timeout:UInt64, callback:(NSArray?) -> Void) -> Void` - Sends a message that requests an acknowledgement from the server. Returns a function which you can use to add a handler. See example. Note: The message is not sent until you call the returned function.
|
||||
6. `emitWithAck(event:String, withItems items:[AnyObject]) -> (UInt64, (NSArray?) -> Void) -> Void` - `emitWithAck` for Objective-C. Note: The message is not sent until you call the returned function.
|
||||
7. `connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection.
|
||||
8. `connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection.
|
||||
8. `connect(#timeoutAfter:Int, withTimeoutHandler handler:(() -> Void)?)` - Connect to the server. If it isn't connected after timeoutAfter seconds, the handler is called.
|
||||
9. `close(#fast:Bool)` - Closes the socket. Once a socket is closed it should not be reopened. Pass true to fast if you're closing from a background task.
|
||||
10. `reconnect()` - Causes the client to reconnect to the server.
|
||||
11. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually.
|
||||
12. `leaveNamespace()` - Causes the client to leave the nsp and go back to /
|
||||
|
||||
Client Events
|
||||
------
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "Socket.IO-Client-Swift"
|
||||
s.version = "2.2.3"
|
||||
s.version = "2.3.1"
|
||||
s.summary = "Socket.IO-client for iOS and OS X"
|
||||
s.description = <<-DESC
|
||||
Socket.IO-client for iOS and OS X.
|
||||
@@ -12,7 +12,7 @@ Pod::Spec.new do |s|
|
||||
s.author = { "Erik" => "nuclear.ace@gmail.com" }
|
||||
s.ios.deployment_target = '8.0'
|
||||
s.osx.deployment_target = '10.10'
|
||||
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.2.3' }
|
||||
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.3.1' }
|
||||
s.source_files = "SocketIOClientSwift/**/*.swift"
|
||||
s.requires_arc = true
|
||||
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files
|
||||
|
||||
@@ -34,6 +34,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray<NSData>?)
|
||||
private typealias ProbeWaitQueue = [Probe]
|
||||
|
||||
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
|
||||
private let workQueue = NSOperationQueue()
|
||||
private let emitQueue = dispatch_queue_create("engineEmitQueue", DISPATCH_QUEUE_SERIAL)
|
||||
private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL)
|
||||
@@ -119,13 +120,14 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
self.pingTimer?.invalidate()
|
||||
self.closed = true
|
||||
|
||||
self.write("", withType: PacketType.CLOSE, withData: nil)
|
||||
self.ws?.disconnect()
|
||||
self.stopPolling()
|
||||
|
||||
if fast || self.polling {
|
||||
self.write("", withType: PacketType.CLOSE, withData: nil)
|
||||
self.client?.engineDidClose("Disconnect")
|
||||
}
|
||||
|
||||
self.stopPolling()
|
||||
}
|
||||
|
||||
private func createBinaryDataForSend(data:NSData) -> (NSData?, String?) {
|
||||
@@ -166,17 +168,16 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
}
|
||||
|
||||
if params != nil {
|
||||
let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
|
||||
|
||||
for (key, value) in params! {
|
||||
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
|
||||
allowedCharacterSet)!
|
||||
self.allowedCharacterSet)!
|
||||
urlPolling += "&\(keyEsc)="
|
||||
urlWebSocket += "&\(keyEsc)="
|
||||
|
||||
if value is String {
|
||||
let valueEsc = (value as! String).stringByAddingPercentEncodingWithAllowedCharacters(
|
||||
allowedCharacterSet)!
|
||||
self.allowedCharacterSet)!
|
||||
urlPolling += "\(valueEsc)"
|
||||
urlWebSocket += "\(valueEsc)"
|
||||
} else {
|
||||
@@ -218,7 +219,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
if self.websocket || self.waitingForPoll || !self.connected {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
self.waitingForPoll = true
|
||||
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!)
|
||||
|
||||
@@ -245,7 +246,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
if this.polling {
|
||||
this.handlePollingFailed(err.localizedDescription)
|
||||
} else {
|
||||
NSLog(err.localizedDescription)
|
||||
SocketLogger.err(err.localizedDescription, client: this)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -262,7 +263,6 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
|
||||
if this.fastUpgrade {
|
||||
this.doFastUpgrade()
|
||||
return
|
||||
} else if !this.closed && this.polling {
|
||||
this.doPoll()
|
||||
}
|
||||
|
||||
@@ -25,14 +25,13 @@
|
||||
import Foundation
|
||||
|
||||
public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient {
|
||||
private lazy var params = [String: AnyObject]()
|
||||
private var anyHandler:((SocketAnyEvent) -> Void)?
|
||||
private var _closed = false
|
||||
private var _connected = false
|
||||
private var _connecting = false
|
||||
private var currentReconnectAttempt = 0
|
||||
private var handlers = ContiguousArray<SocketEventHandler>()
|
||||
private var paramConnect = false
|
||||
private var params:[String: AnyObject]?
|
||||
private var _secure = false
|
||||
private var _sid:String?
|
||||
private var _reconnecting = false
|
||||
@@ -88,11 +87,16 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
self.socketURL = socketURL
|
||||
self.opts = opts
|
||||
|
||||
|
||||
// Set options
|
||||
if let sessionDelegate = opts?["sessionDelegate"] as? NSURLSessionDelegate {
|
||||
self.sessionDelegate = sessionDelegate
|
||||
}
|
||||
|
||||
if let connectParams = opts?["connectParams"] as? [String: AnyObject] {
|
||||
self.params = connectParams
|
||||
}
|
||||
|
||||
if let log = opts?["log"] as? Bool {
|
||||
self.log = log
|
||||
}
|
||||
@@ -156,37 +160,38 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
Connect to the server.
|
||||
*/
|
||||
public func connect() {
|
||||
if self.closed {
|
||||
println("Warning! This socket was previously closed. This might be dangerous!")
|
||||
self._closed = false
|
||||
}
|
||||
|
||||
if self.connected {
|
||||
return
|
||||
}
|
||||
|
||||
self.addEngine()
|
||||
self.engine?.open()
|
||||
self.connect(timeoutAfter: 0, withTimeoutHandler: nil)
|
||||
}
|
||||
|
||||
/**
|
||||
Connect to the server with params that will be passed on connection.
|
||||
Connect to the server. If we aren't connected after timeoutAfter, call handler
|
||||
*/
|
||||
public func connectWithParams(params:[String: AnyObject]) {
|
||||
public func connect(#timeoutAfter:Int, withTimeoutHandler handler:(() -> Void)?) {
|
||||
if self.closed {
|
||||
println("Warning! This socket was previously closed. This might be dangerous!")
|
||||
SocketLogger.log("Warning! This socket was previously closed. This might be dangerous!", client: self)
|
||||
self._closed = false
|
||||
}
|
||||
|
||||
if self.connected {
|
||||
} else if self.connected {
|
||||
return
|
||||
}
|
||||
|
||||
self.params = params
|
||||
self.paramConnect = true
|
||||
|
||||
self.addEngine()
|
||||
self.engine?.open(opts: params)
|
||||
self.engine?.open(opts: self.params)
|
||||
|
||||
if timeoutAfter == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutAfter) * Int64(NSEC_PER_SEC))
|
||||
|
||||
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in
|
||||
if let this = self where !this.connected {
|
||||
this._closed = true
|
||||
this._connecting = false
|
||||
this.engine?.close(fast: true)
|
||||
|
||||
handler?()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func createOnAck(event:String, items:[AnyObject]) -> OnAckCallback {
|
||||
@@ -399,7 +404,20 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
}
|
||||
}
|
||||
|
||||
func joinNamespace() {
|
||||
/**
|
||||
Leaves nsp and goes back to /
|
||||
*/
|
||||
public func leaveNamespace() {
|
||||
if self.nsp != "/" {
|
||||
self.engine?.send("1/\(self.nsp)", withData: nil)
|
||||
self.nsp = "/"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Joins nsp if it is not /
|
||||
*/
|
||||
public func joinNamespace() {
|
||||
SocketLogger.log("Joining namespace", client: self)
|
||||
|
||||
if self.nsp != "/" {
|
||||
@@ -489,10 +507,6 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
isInternalMessage: true)
|
||||
|
||||
self.currentReconnectAttempt++
|
||||
if self.paramConnect {
|
||||
self.connectWithParams(self.params)
|
||||
} else {
|
||||
self.connect()
|
||||
}
|
||||
self.connect()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user