Swift/iOS8: 为 websocket SocketIOCocoa 创建共享实例
Swift/iOS8: Create shared instance for websocket SocketIOCocoa
我正在尝试使用此处找到的 SocketIOCocoa 库为 websocket 客户端创建一个共享实例:
https://github.com/shuoli84/SocketIOCocoa
当在 ViewController 中用于创建 websocket 客户端的新实例时,该库工作得非常好 - 但我无法设置共享实例。我尝试了什么:
import Foundation
import Alamofire
public class WebSocketClient:SocketIOSocket {
public var WebSocket:SocketIOClient!
func myWebSocketClient() -> SocketIOClient {
if WebSocket != nil {
return WebSocket
} else {
let uri = "\(CurrentConfiguration.serverURL)/socket.io/"
var client = SocketIOClient(uri: uri, reconnect: true, timeout:3000)
client.open()
return client
}
}
由于我对 protocols/shared 实例的使用还很陌生,所以我不知道我是否走在正确的轨道上。至少当我想将它用作:
var foo = WebSocketClient.myWebSocketclient()
这就是我一次完成的方法 VC:
func newSocket() {
let uri = "\(CurrentConfiguration.serverURL)/socket.io/"
var client = SocketIOClient(uri: uri, reconnect: true, timeout:3000)
client.open()
var socket = client.socket("myNamespace") //join namespace
socket.event("customEvent", data: websocketRoomData){ (packet) -> Void in
}//emit event after join
socket.delegate = self
}
func socketOnEvent(socket: SocketIOSocket, event: String, data: AnyObject?) {
switch event {
case "returning from server event":
println(event)
let returnData = data as? NSArray
//do stuff with return data
doStuff(returnData!)
default:
println("Error: unrecognized Websockets Event received!")
}
}
例如。
您可以在代码中的任何位置使用 SocketSingleton.call。
让套接字:SocketSingleton = SocketSingleton.call
socket.socketEmit()
class SocketSingleton: NSObject {
static let call = SocketSingleton()
let socket = SocketIOClient(socketURL: NSURL(string:"url")!)
override init() {
super.init()
addHandlers()
socket.connect()
}
func addHandlers() {
socket.on("message") { data, ack in
}
socket.on("reconnect") { data, ack in
}
socket.on("error") { data, ack in
}
}
func socketEmit() {
let msg = [:]
var sendJson : NSString = ""
do {
let json = try NSJSONSerialization.dataWithJSONObject(msg, options: NSJSONWritingOptions.PrettyPrinted)
let string = NSString(data: json, encoding: NSUTF8StringEncoding)
sendJson = string! as NSString
} catch let error as NSError {
print(error.description)
}
socket.emit("message", sendJson)
}
}
我正在尝试使用此处找到的 SocketIOCocoa 库为 websocket 客户端创建一个共享实例: https://github.com/shuoli84/SocketIOCocoa
当在 ViewController 中用于创建 websocket 客户端的新实例时,该库工作得非常好 - 但我无法设置共享实例。我尝试了什么:
import Foundation
import Alamofire
public class WebSocketClient:SocketIOSocket {
public var WebSocket:SocketIOClient!
func myWebSocketClient() -> SocketIOClient {
if WebSocket != nil {
return WebSocket
} else {
let uri = "\(CurrentConfiguration.serverURL)/socket.io/"
var client = SocketIOClient(uri: uri, reconnect: true, timeout:3000)
client.open()
return client
}
}
由于我对 protocols/shared 实例的使用还很陌生,所以我不知道我是否走在正确的轨道上。至少当我想将它用作: var foo = WebSocketClient.myWebSocketclient()
这就是我一次完成的方法 VC:
func newSocket() {
let uri = "\(CurrentConfiguration.serverURL)/socket.io/"
var client = SocketIOClient(uri: uri, reconnect: true, timeout:3000)
client.open()
var socket = client.socket("myNamespace") //join namespace
socket.event("customEvent", data: websocketRoomData){ (packet) -> Void in
}//emit event after join
socket.delegate = self
}
func socketOnEvent(socket: SocketIOSocket, event: String, data: AnyObject?) {
switch event {
case "returning from server event":
println(event)
let returnData = data as? NSArray
//do stuff with return data
doStuff(returnData!)
default:
println("Error: unrecognized Websockets Event received!")
}
}
例如。 您可以在代码中的任何位置使用 SocketSingleton.call。
让套接字:SocketSingleton = SocketSingleton.call
socket.socketEmit()
class SocketSingleton: NSObject {
static let call = SocketSingleton()
let socket = SocketIOClient(socketURL: NSURL(string:"url")!)
override init() {
super.init()
addHandlers()
socket.connect()
}
func addHandlers() {
socket.on("message") { data, ack in
}
socket.on("reconnect") { data, ack in
}
socket.on("error") { data, ack in
}
}
func socketEmit() {
let msg = [:]
var sendJson : NSString = ""
do {
let json = try NSJSONSerialization.dataWithJSONObject(msg, options: NSJSONWritingOptions.PrettyPrinted)
let string = NSString(data: json, encoding: NSUTF8StringEncoding)
sendJson = string! as NSString
} catch let error as NSError {
print(error.description)
}
socket.emit("message", sendJson)
}
}