dumbledad
dumbledad

Reputation: 17525

Starting a web socket server on iOS (with Starscream?)

I'm trying to build an iOS app that serves up frames from the camera over a web socket. Starscream seems to be the library of choice for web sockets on iOS. All the Starscream examples I can find (e.g. here or here) start with a line like this one:

socket = WebSocket(url: URL(string: "ws://localhost:8080/")!)

I.e. they assume that we are opening a connection to a server, and after that we can send and receive data over the web socket. What I need is the other way around, I will be serving the data over a web socket and a client will connect to me so that I can send the data over a web socket.

Is that possible in iOS, potentially using Starscream? How?

Upvotes: 0

Views: 3814

Answers (1)

user2959950
user2959950

Reputation: 1

Yes, from Starscream you can found WebSocketServer and start it from iOS application or Mac-OS application:

import Starscream

let webSocket = WebSocketServer()

webSocket.onEvent = { event in
    print("Event! \(event)")
}

let error = webSocket.start(address: "ws://localhost", port: 5020)

if let error {
    print("Error: \(error)")
} else {
    print("Server started")
}

while true {
   try await Task.sleep(for: .seconds(1_000_000))
}

Upvotes: 0

Related Questions