sockets - Swift: How to update UI when listening to a stream -
code below seems work, except can't seem update button
after getting stream event.
i've tried both delegate
, dispatch_async
, neither seem trick.
what magic missing here?
import uikit class testviewcontroller: uiviewcontroller { @iboutlet weak var connectbutton: uibarbuttonitem! @iboutlet weak var stopbutton: uibarbuttonitem! let socket = socket() override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() } @ibaction func connectserver(sender: uibarbuttonitem) { socket.connect("10.0.0.1", port: 12345) } func setstopbuttonstate(state: string) { switch state { case "go": stopbutton?.title = "stop" break case "stop": stopbutton?.title = "start" break default: break } } } class socket: nsobject, nsstreamdelegate { var inputstream: nsinputstream? var outputstream: nsoutputstream? func connect(host: string, port: int) { nsstream.getstreamstohostwithname(host, port: port, inputstream: &inputstream, outputstream: &outputstream) if inputstream != nil && outputstream != nil { inputstream!.delegate = self outputstream!.delegate = self inputstream!.scheduleinrunloop(.mainrunloop(), formode: nsdefaultrunloopmode) outputstream!.scheduleinrunloop(.mainrunloop(), formode: nsdefaultrunloopmode) inputstream!.open() outputstream!.open() } } func stream(astream: nsstream, handleevent eventcode: nsstreamevent) { if astream === inputstream { switch eventcode { case nsstreamevent.hasbytesavailable: var inputbuffer = array<uint8>(count:2048, repeatedvalue: 0) inputstream?.read(&inputbuffer, maxlength: 2048) testviewcontroller().setstopbuttonstate(string(bytes: inputbuffer, encoding: nsutf8stringencoding)!) break default: break } } } }
let socket have callback , , call when stream have bytes :
import uikit class testviewcontroller: uiviewcontroller { @iboutlet weak var connectbutton: uibarbuttonitem! @iboutlet weak var stopbutton: uibarbuttonitem! let socket = socket() override func viewdidload() { super.viewdidload() } override func didreceivememorywarning() { super.didreceivememorywarning() } @ibaction func connectserver(sender: uibarbuttonitem) { socket.connect("10.0.0.1", port: 12345) socket.changewhenhasbytesavailable = { str in // change string } } func setstopbuttonstate(state: string) { switch state { case "go": stopbutton?.title = "stop" break case "stop": stopbutton?.title = "start" break default: break } } } class socket: nsobject, nsstreamdelegate { var inputstream: nsinputstream? var outputstream: nsoutputstream? var changewhenhasbytesavailable: ((string)->())? func connect(host: string, port: int) { nsstream.getstreamstohostwithname(host, port: port, inputstream: &inputstream, outputstream: &outputstream) if inputstream != nil && outputstream != nil { inputstream!.delegate = self outputstream!.delegate = self inputstream!.scheduleinrunloop(.mainrunloop(), formode: nsdefaultrunloopmode) outputstream!.scheduleinrunloop(.mainrunloop(), formode: nsdefaultrunloopmode) inputstream!.open() outputstream!.open() } } func stream(astream: nsstream, handleevent eventcode: nsstreamevent) { if astream === inputstream { switch eventcode { case nsstreamevent.hasbytesavailable: var inputbuffer = array<uint8>(count:2048, repeatedvalue: 0) inputstream?.read(&inputbuffer, maxlength: 2048) //call in main queue dispatch_async(dispatch_get_main_queue(), { self.changewhenhasbytesavailable?(string(bytes: inputbuffer, encoding: nsutf8stringencoding)!) }) break default: break } } } }
Comments
Post a Comment