1

I am trying to setup server using Vapor. As client, I have simple iOS app using NSUrlSession - URLSessionWebSocketTask. My question is... how I can set session.data from iOS app?

iOS App - Connect method

func connect(completion: @escaping ()->() = { }) {
        guard webSocketTask == nil else { return }

        self.username = "Name"
        self.userID = UUID().uuidString

        let url = URL(string: "ws://localhost:8080/connect")!
        webSocketTask = URLSession.shared.webSocketTask(with: url)
        webSocketTask?.receive(completionHandler: onReceive)
        webSocketTask?.resume()
        
    }

Vapor:

app.webSocket("connect") { request, ws in
        let controller = Controller()

        let userName = request.session.data["nickname"] ?? "Unknown user"
        let data = request.session.data["data"] ?? "Empty Data"

        controller.addUser(userName, with: room, withConnection: ws)
.....
....
...
..
.
George Heints
  • 1,303
  • 3
  • 20
  • 37

1 Answers1

1

You may use NSURLSessionDataTask.

https://developer.apple.com/documentation/foundation/nsurlsessiondatatask

Moose
  • 2,607
  • 24
  • 23
  • Can you provide code example, please? I know that NSURLSessionDataTask can be used to receive data, but not sure that I can transfer data with it to server based on Vapor? Anyway code example will be appreciated! – George Heints Nov 02 '21 at 13:55
  • Unfortunately, I can't right now. But I will come back later on this page. I'm sorry for this. I was passing by and just gave you a quick tip. Why a vapor based server may differ from any other server? That was 2 days ago, is it fixed? – Moose Nov 04 '21 at 18:23
  • It's not about Vapor, it's about iOS, because iOS should pass some params to Server on connect. It's not fixed yet, still looking for solution. Also tried cookies, socket.io params... no luck for now. If you have free time to check it.. – George Heints Nov 05 '21 at 08:14