How do we create a TLS websocket with Network framework?

nw_parameters_configure_protocol_block_t configure_tls = ^(nw_protocol_options_t tls_options) {
sec_protocol_options_t sec_options = nw_tls_copy_sec_protocol_options(tls_options);

NSData *data_psk = [@"abc" dataUsingEncoding:NSASCIIStringEncoding];
NSData *data_client_id = [@"test" dataUsingEncoding:NSASCIIStringEncoding];

dispatch_data_t psk = dispatch_data_create([data_psk bytes], [data_psk length], nil, DISPATCH_DATA_DESTRUCTOR_DEFAULT);

dispatch_data_t client_id = dispatch_data_create([data_client_id bytes], [data_client_id length], nil, DISPATCH_DATA_DESTRUCTOR_DEFAULT);

sec_protocol_options_append_tls_ciphersuite(sec_options, (SSLCipherSuite)TLS_PSK_WITH_AES_256_GCM_SHA384);

sec_protocol_options_add_pre_shared_key(sec_options, psk, client_id);

sec_protocol_options_set_tls_pre_shared_key_identity_hint(sec_options, client_id);

nw_parameters_t parameters = nw_parameters_create_secure_tcp(configure_tls, NW_PARAMETERS_DEFAULT_CONFIGURATION);
nw_endpoint_t endpoint = nw_endpoint_create_host("127.0.0.1", "8888");

The code above can be used to establish a connection to an endpoint using TLS-PSK but how do we specify the options that we wish to use a WebSocket connection? if we specify ws options with nw_ws_create_options but there's no documentation to guide me where the nw_protocol_options_t should then be placed- also; after that is configured can we expect data to send and receive with the existing nw_connection_send and nw_connection_receive methods? If you could share some sample code on this implementation it would be very helpful.

Answered by DTS Engineer in 691761022

Matt’s Configuring a Wi-Fi Accessory to Join the User’s Network sample shows how to use WebSocket with TLS PSK.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Matt’s Configuring a Wi-Fi Accessory to Join the User’s Network sample shows how to use WebSocket with TLS PSK.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

is there a copy of this code's Objective-C equivalent though?

No. However, it seems like you already know how to use nw_connection_t in general, and so you just need to translate the stuff specific to WebSockets. That should be pretty straightforward, but if you hit a snag please post back with the details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How do we create a TLS websocket with Network framework?
 
 
Q