I use to develop for IOS and currently handling MacOS development for the first time. I use websocket in my app before acting as clients. However, I never had to make a websocket server using swift.
I was wondering if it is possible to let my Mac App to act as a web socket server to receive informations from web socket clients? I have been trying to find resources, but most of them are about making clients instead of server. I would appreciate any help on finding resources to help me build this feature.
Thank you
Post
Replies
Boosts
Views
Activity
I been looking at Network api for apple and was able to setup web socket connection with tcp. I believe this creates ws:// instead of wss://. So I have been looking at tls connection but having a trouble figuring out how to set up NWParameters.tls. my goal is to create wss:// web socket server on MacOS.
my init for my server is
self.port = NWEndpoint.Port(rawValue: port)!
sec_protocol_options_append_tls_ciphersuite(NWProtocolTLS.Options().securityProtocolOptions,
tls_ciphersuite_t(rawValue:mUInt16((TLS_PSK_WITH_AES_128_GCM_SHA256)))!)
parameters = NWParameters.tls
parameters.allowLocalEndpointReuse = true
parameters.includePeerToPeer = true
let wsOptions = NWProtocolWebSocket.Options()
wsOptions.autoReplyPing = true parameters.defaultProtocolStack.applicationProtocols.insert(wsOptions, at: 0)
listener = try! NWListener(using: parameters, on: self.port)
Using the tic-tac-toe example, I thought I was missing sec_protocol_options_append_tls_ciphersuite, but it doesn't allow me connecting to my server. Do I need authenticationKey convert it to HMAC.authenticationCode then get Dispatch Data and use sec_protocol_options_add_pre_shared_key? or am I missing different kind of certification?
I am using https://www.piesocket.com/websocket-tester to test my connection currently.
is it possible for a macOS app created using swiftUI on Xcode to have an option, which if the Mac starts up the app will automatically starts as well?
Hi, I am trying to setup TLS web socket but I am getting NO_SHARED_CIPHER during handshakes. normal WS server connects properly but I need to get it into WSS version. I believe I am missing some sorts of certificates (maybe like P12?) but I have never set up TLS before. How do I import certificates or which sec_protocol_options should I use?
my current TLS code is this
init(port: UInt16) {
//15881 ws 15882 wss
self.port = NWEndpoint.Port(rawValue: 15882)!
let workQueue = DispatchQueue(label: "mqtt")
parameters = NWParameters(tls: SwiftWebSocketServer.tlsOptions(psk: "1234", pskIdentity: "1234", queue: workQueue))
//parameters = NWParameters(tls: nil)
parameters.allowLocalEndpointReuse = true
parameters.includePeerToPeer = true
let wsOptions = NWProtocolWebSocket.Options()
wsOptions.autoReplyPing = true
parameters.defaultProtocolStack.applicationProtocols.insert(wsOptions, at: 0)
listener = try! NWListener(using: parameters, on: self.port)
}
private static func tlsOptions(psk: String, pskIdentity: String,queue: DispatchQueue) -> NWProtocolTLS.Options {
let tlsOptions = NWProtocolTLS.Options()
let allowInsecure = true
// let pskData = Data(psk.utf8)
let authenticationKey = SymmetricKey(data: psk.data(using: .utf8)!)
var authenticationCode = HMAC<SHA256>.authenticationCode(for: "1234".data(using: .utf8)!, using: authenticationKey)
let authenticationDispatchData = withUnsafeBytes(of: &authenticationCode) { (ptr: UnsafeRawBufferPointer) in
DispatchData(bytes: ptr)
}
let pskIdentityData = Data(pskIdentity.utf8)
let pskIdentityDispatchData = pskIdentityData.withUnsafeBytes { buf in
DispatchData(bytes: buf)
}
sec_protocol_options_set_min_tls_protocol_version(tlsOptions.securityProtocolOptions, .TLSv12)
sec_protocol_options_append_tls_ciphersuite(
tlsOptions.securityProtocolOptions,
tls_ciphersuite_t(rawValue: UInt16(TLS_PSK_WITH_AES_128_CBC_SHA256))!
)
sec_protocol_options_append_tls_ciphersuite(
tlsOptions.securityProtocolOptions,
tls_ciphersuite_t(rawValue: UInt16(TLS_PSK_WITH_AES_128_GCM_SHA256))!
)
sec_protocol_options_append_tls_ciphersuite(
tlsOptions.securityProtocolOptions,
tls_ciphersuite_t(rawValue: UInt16(TLS_PSK_WITH_AES_256_CBC_SHA384))!
)
sec_protocol_options_append_tls_ciphersuite(
tlsOptions.securityProtocolOptions,
tls_ciphersuite_t(rawValue: UInt16(TLS_PSK_WITH_AES_256_GCM_SHA384))!
)
sec_protocol_options_set_verify_block(tlsOptions.securityProtocolOptions, { (sec_protocol_metadata, sec_trust, sec_protocol_verify_complete) in
let trust = sec_trust_copy_ref(sec_trust).takeRetainedValue()
var error: CFError?
if SecTrustEvaluateWithError(trust, &error) {
sec_protocol_verify_complete(true)
} else {
if allowInsecure == true {
sec_protocol_verify_complete(true)
} else {
sec_protocol_verify_complete(false)
}
}
}, queue)
sec_protocol_options_set_peer_authentication_required(tlsOptions.securityProtocolOptions, false)
sec_protocol_options_add_pre_shared_key(
tlsOptions.securityProtocolOptions,
authenticationDispatchData as __DispatchData,
pskIdentityDispatchData as __DispatchData
)
return tlsOptions
}
``
So currently I using blackhole program to connect the output into input device. I can see that ReplayKit is able to pickup the input and get audio buffer. however, there is no data in the buffer. if there is no data, does that mean there s no audio recorded, or the audio data is store somewhere else?
Hello
I am trying to setup web socket secure server.
I have used telegraph in order to set it up but I would like to be able to do it without the library.
I believe I have basic setup as I can setup with WS. I am currently getting No_Certificate_set error while using networkAPI.
So I want to know how do I setup certificate using the Apple's networkAPI?
hello,
I am currently working on a macOS application in swiftUI. We have a window version of the app which uses certain mathematical functions and would like to use it for our Mac version in order to reduce work and maintain same results through all different versions. We have the c# code and dylib created but I have never dealt with this so I would like any sorts of help I can get.
I currently have dylib added to my project and added them on embedded framework and such but unsure what else I need to do.
Thank you.
hello,
I am using dylibs and using c library.
I am opening the dylibs and using some of the function properly. However, some of the other functions are giving me errors relating to freeing pointers.
here is one of the function below.
first I open the dl and save it as variable in a class
let dylib = dlopen("x.dylib", RTLD_NOW)
then i create type alias for the function
typealias LoadConfiguration = @convention(c) (ath_conf_t)> -> Void
then here is the function to call the function of c
func LoadConfigLib(athConfT : inout ath_conf_t) -> Void?{
guard dylib != nil else {
errorDylib()
return nil
}
guard let sym = dlsym(dylib, "LoadConfiguration") else {
errorDylib()
return nil
}
let f = unsafeBitCast(sym, to: LoadConfiguration.self)
let result: Void = f(athConfT)
print(result)
return result
}
here is the error.
malloc: *** error for object 0x102665cc0: pointer being freed was not allocated
malloc: *** set a breakpoint in malloc_error_break to debug things
I am not certain how to fix this problem or how to approach this.
any sorts of guidance would be appreciated
here is the ath_conf_t
public class ath_conf_t : NSObject {
public var version: CInt
public var channels: NSArray
init(version: CInt = 0, channels: NSArray = Array(repeating: ath_channel_t.init(arg1: false, arg2: 0, adaptiveHigh: 0, arg3: 0), count: constant) as NSArray) {
self.version = version
self.channels = channels
}
}
Say I have an example struct in C++ that is inside dylib like this
typedef struct {
int a;
} addingTemp;
and I need to use it for a function that adds 2 together, and store it into 3rd value
void add(addingTemp a, addingTemp b, addingTemp& c) {
c.a = a.a + b.a;
}
how do I create the struct in swift?
this is my adding library function in swift
typealias add = @convention(c) (addingTemp, addingTemp, UnsafeMutablePointer<addingTemp>) -> Void
func addLib(a : addingTemp, b: addingTemp, out: inout addingTemp) -> Void{
guard dylib != nil else {
errorDylib()
return
}
guard let sym = dlsym(dylib, "add") else {
errorDylib()
return
}
let f = unsafeBitCast(sym, to:add.self )
let result : Void = f(a ,b, &out)
// closeDylib()
return
}
this is my current implementation
public class addingTemp: NSObject {
public var a : CInt
init(a: CInt = 0) {
self.a = a
}
}
but it gives me memory access error, it looks like it isn't being stored properly as the type doesn't match.
DLL_PUBLIC void add(
addingTemp a,
addingTemp b,
addingTemp* c,);
the function works when I only use Int instead of addingTemp class.
Also, when the C++ function is calculating, instead of a.a = 1, b.a = 1, it outputs large numbers such as a.a = 389179664 b.a = 389179344