Re: Still interested in (NS)URLSessionStreamTask
Re: Still interested in (NS)URLSessionStreamTask
- Subject: Re: Still interested in (NS)URLSessionStreamTask
- From: "Quinn \"The Eskimo!\"" <email@hidden>
- Date: Tue, 07 Feb 2017 23:09:18 +0000
On 7 Feb 2017, at 20:40, Daryle Walker <email@hidden> wrote:
> Do session tasks still use resume …?
Yes.
> If resume is used, how would you open a connection in implicit-SSL mode?
I’m not sure what you mean by “implicit-SSL mode” but:
* If you’re working with a protocol that assumes TLS (like HTTPS), call `startSecureConnection()` before doing any I/O on the stream
* If you’re working with a protocol that uses STARTTLS, there’s comments in <Foundation/NSURLSession.h> that explain how `startSecureConnection()` interacts with pending I/O.
Pasted in below is a simple example of how to use this API. There’s a few lines that you can uncomment to test TLS.
Share and Enjoy
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
---------------------------------------------------------------------------
@IBAction func startStopAction(_ sender: Any) {
if let stream = self.stream {
self.stop(stream: stream)
} else {
self.start()
}
}
lazy var session: URLSession = {
let config = URLSessionConfiguration.default
return URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
}()
var stream: URLSessionStreamTask? = nil
func start() {
NSLog("start")
// let stream = self.session.streamTask(withHostName: "imap.mail.me.com", port: 993)
let stream = self.session.streamTask(withHostName: "sully.local.", port: 12345)
self.stream = stream
stream.resume()
// stream.startSecureConnection()
self.startRead(stream: stream)
let hello = "a001 NOOP\r\n".data(using: .utf8)!
stream.write(hello, timeout: 60.0) { (error) in
if let error = error {
NSLog("write error")
} else {
NSLog("write complete")
}
}
}
func startRead(stream: URLSessionStreamTask) {
stream.readData(ofMinLength: 1, maxLength: 2048, timeout: 60.0) { (data, eof, error) in
if let error = error {
NSLog("read error")
}
if let data = data {
NSLog("read data %@", data as NSData)
}
NSLog("read eof %@", eof ? "true" : "false")
if error == nil && !eof {
self.startRead(stream: stream)
}
}
}
func stop(stream: URLSessionStreamTask) {
NSLog("stop")
stream.closeRead()
stream.closeWrite()
self.stream = nil
}
---------------------------------------------------------------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden