merge master

This commit is contained in:
Erik 2015-03-16 16:08:49 -04:00
commit 937090458c
6 changed files with 213 additions and 111 deletions

View File

@ -38,9 +38,11 @@ import Socket_IO_Client_Swift
API API
=== ===
Constructor Constructors
----------- -----------
`init(socketURL: String, opts:[String: AnyObject]? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example) `init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example)
`convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example.
Methods Methods
------- -------
1. `socket.on(name:String, callback:((data:NSArray?, ack:AckEmitter?) -> Void))` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example. 1. `socket.on(name:String, callback:((data:NSArray?, ack:AckEmitter?) -> Void))` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example.
@ -124,12 +126,12 @@ socket.connect()
Objective-C Example Objective-C Example
=================== ===================
```objective-c ```objective-c
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" opts:nil]; SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil];
[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) { [socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) {
NSLog(@"connected"); NSLog(@"connected");
[socket emitObjc:@"echo" :@[@"echo test"]]; [socket emitObjc:@"echo" :@[@"echo test"]];
[[socket emitWithAckObjc:@"ackack" :@[@"test"]] onAck:^(NSArray* data) { [[socket emitWithAckObjc:@"ackack" :@[@"test"]] onAck:0 withCallback:^(NSArray* data) {
NSLog(@"Got data"); NSLog(@"Got data");
}]; }];
}]; }];

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "Socket.IO-Client-Swift" s.name = "Socket.IO-Client-Swift"
s.version = "1.1.4" s.version = "1.2.0"
s.summary = "Socket.IO-client for Swift" s.summary = "Socket.IO-client for Swift"
s.description = <<-DESC s.description = <<-DESC
Socket.IO-client for Swift. Socket.IO-client for Swift.
@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.author = { "Erik" => "nuclear.ace@gmail.com" } s.author = { "Erik" => "nuclear.ace@gmail.com" }
s.ios.deployment_target = '8.0' s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10' s.osx.deployment_target = '10.10'
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v1.1.4' } s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v1.2.0' }
s.source_files = "SwiftIO/**/*.swift" s.source_files = "SwiftIO/**/*.swift"
s.requires_arc = true s.requires_arc = true
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files # s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files

View File

@ -179,8 +179,9 @@ public class SocketEngine: NSObject, WebSocketDelegate {
// println(data) // println(data)
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) {
dispatch_async(self!.parseQueue) {callback(str as String)} if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
dispatch_async(self!.parseQueue) {callback(str)}
} }
self?.waitingForPoll = false self?.waitingForPoll = false
@ -406,6 +407,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
private func parseEngineMessage(var message:String) { private func parseEngineMessage(var message:String) {
// NSLog("Engine got message: \(message)") // NSLog("Engine got message: \(message)")
fixDoubleUTF8(&message)
// We should upgrade // We should upgrade
if message == "3probe" { if message == "3probe" {

100
SwiftIO/SocketFixUTF8.swift Normal file
View File

@ -0,0 +1,100 @@
//
// SocketFixUTF8.swift
// Socket.IO-Swift
//
// Created by Erik Little on 3/16/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.
//
// Adapted from: https://github.com/durbrow/fix-double-utf8.swift
import Foundation
var memoizer = [String: UnicodeScalar]()
func lookup(base:UnicodeScalar, combi:UnicodeScalar) -> UnicodeScalar {
let combined = "\(base)\(combi)"
if let y = memoizer[combined] {
return y
}
for i in 0x80...0xFF {
let ch = UnicodeScalar(i)
if String(ch) == combined {
memoizer[combined] = ch
return ch
}
}
let ch = UnicodeScalar(0xFFFD) // Unicode replacement character <EFBFBD>
memoizer[combined] = ch
return ch
}
func fixDoubleUTF8(inout name:String) {
var isASCII = true
var y = [UInt8]()
for ch in name.unicodeScalars {
if ch.value < 0x80 {
y.append(UInt8(ch))
continue
}
isASCII = false
if ch.value < 0x100 {
y.append(UInt8(ch))
continue
}
// might be a combining character that when combined with the
// preceeding character maps to a codepoint in the UTF8 range
if y.count == 0 {
return
}
let last = y.removeLast()
let repl = lookup(UnicodeScalar(last), ch)
// the replacement needs to be in the UTF8 range
if repl.value >= 0x100 {
return
}
y.append(UInt8(repl))
}
if isASCII {
return
}
y.append(0) // null terminator
return y.withUnsafeBufferPointer {
let cstr = UnsafePointer<CChar>($0.baseAddress) // typecase from uint8_t * to char *
let rslt = String.fromCStringRepairingIllFormedUTF8(cstr) // -> (String, Bool)
if let str = rslt.0 {
if !rslt.hadError {
name = str
}
}
return
}
}

View File

@ -74,7 +74,7 @@ public class SocketIOClient: NSObject {
return self._sid return self._sid
} }
public init(var socketURL:String, opts:[String: AnyObject]? = nil) { public init(var socketURL:String, opts:NSDictionary? = nil) {
if socketURL["https://"].matches().count != 0 { if socketURL["https://"].matches().count != 0 {
self._secure = true self._secure = true
} }
@ -116,6 +116,10 @@ public class SocketIOClient: NSObject {
self.engine = SocketEngine(client: self, forcePolling: self.forcePolling) self.engine = SocketEngine(client: self, forcePolling: self.forcePolling)
} }
public convenience init(socketURL:String, options:NSDictionary?) {
self.init(socketURL: socketURL, opts: options)
}
// Closes the socket // Closes the socket
public func close() { public func close() {
self._closed = true self._closed = true

View File

@ -217,20 +217,16 @@ class SocketParser {
return return
} }
/** /**
Begin check for message Begin check for message
**/ **/
let messageGroups = stringMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$"].groups() let messageGroups = stringMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$",
NSRegularExpressionOptions.DotMatchesLineSeparators].groups()
if messageGroups == nil { if messageGroups == nil {
NSLog("Error in groups") NSLog("Error in groups")
return return
} }
// let messageGroups = SwiftRegex(target: stringMessage as NSString,
// pattern: "(\\d*)\\/?(\\w*)?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$",
// options: NSRegularExpressionOptions.DotMatchesLineSeparators).groups()
if messageGroups![1].hasPrefix("2") { if messageGroups![1].hasPrefix("2") {
var mesNum = messageGroups![1] var mesNum = messageGroups![1]
var ackNum:String var ackNum:String
@ -343,10 +339,8 @@ class SocketParser {
/** /**
Begin check for binary placeholders Begin check for binary placeholders
**/ **/
let binaryGroup = message["^(\\d*)-\\/?(\\w*)?,?(\\d*)?\\[(\".*?\")?,?(.*)?\\]$"].groups() let binaryGroup = message["^(\\d*)-\\/?(\\w*)?,?(\\d*)?\\[(\".*?\")?,?(.*)?\\]$",
// let binaryGroup = SwiftRegex(target: message, NSRegularExpressionOptions.DotMatchesLineSeparators].groups()
// pattern: "^(\\d*)-\\/?(\\w*)?,?(\\d*)?\\[(\".*?\")?,?(.*)?\\]$",
// options: NSRegularExpressionOptions.DotMatchesLineSeparators).groups()
if binaryGroup == nil { if binaryGroup == nil {
return return