ios - Swift/iOS8: Create shared instance for websocket SocketIOCocoa -


i trying create shared instance websocket client, using socketiococoa library found here: https://github.com/shuoli84/socketiococoa

the library works fine, when used inside viewcontroller create new instance of websocket client - somehow fail set shared instance. tried:

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         }     } 

as quite new use of protocols/shared instances dont know if on right track. @ least doesnt work when want use as: var foo = websocketclient.mywebsocketclient()

this how in single 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 server event":             println(event)             let returndata = data as? nsarray              //do stuff return data              dostuff(returndata!)          default:             println("error: unrecognized websockets event received!")     } } 

for example. can use socketsingleton.call place in code.

let socket: 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 = ""     {         let json = try nsjsonserialization.datawithjsonobject(msg, options: nsjsonwritingoptions.prettyprinted)         let string = nsstring(data: json, encoding: nsutf8stringencoding)         sendjson = string! nsstring     } catch let error nserror {         print(error.description)     }      socket.emit("message", sendjson) }  } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -