socket.io swift client emit with callback function

Viewed 204

We have a socket.io v2 server running and mobile apps are currently communicating with this server successfully. I now want to migrate the watchOS 6.0 app from REST services to socket.io.

I imported SocketIO-Swift-Client 15.0.0 and Starscream 3.1.1 and assigned my manager and established a connection.

init(){
    socket = manager.defaultSocket
    socket.connect()
}

let manager = SocketManager(socketURL: URL(string: "http://XXXXXX")!, config: [.log(true), .compress])
var socket:SocketIOClient!

2021-06-08 12:56:51.646433+0200 someApp WatchKit Extension[18187:708411] LOG SocketIOClient{/}: Handling event: ping with data: []
2021-06-08 12:56:51.646433+0200 someApp WatchKit Extension[18187:709273] LOG SocketEngine: Writing poll:  has data: false
2021-06-08 12:56:51.646599+0200 someApp WatchKit Extension[18187:709273] LOG SocketEnginePolling: Sending poll:  as type: 2
2021-06-08 12:56:51.647051+0200 someApp WatchKit Extension[18187:709273] LOG SocketEnginePolling: Created POST string: 1:2
2021-06-08 12:56:51.647580+0200 someApp WatchKit Extension[18187:709273] LOG SocketEnginePolling: POSTing
2021-06-08 12:56:51.647902+0200 someApp WatchKit Extension[18187:709273] LOG SocketEnginePolling: Doing polling POST http://someAdress/socket.io/?transport=polling&b64=1&sid=cGzUF60eLZLoF5fAAADA
2021-06-08 12:56:51.823879+0200 someApp WatchKit Extension[18187:709305] LOG SocketEnginePolling: Got polling response
2021-06-08 12:56:51.824562+0200 someApp WatchKit Extension[18187:709305] LOG SocketEnginePolling: Got poll message: 1:3
2021-06-08 12:56:51.825178+0200 someApp WatchKit Extension[18187:709305] LOG SocketEngine: Got message: 3
2021-06-08 12:56:51.826034+0200 someApp WatchKit Extension[18187:708411] LOG SocketIOClient{/}: Handling event: pong with data: []
2021-06-08 12:56:51.826128+0200 someApp WatchKit Extension[18187:709305] LOG SocketEnginePolling: Doing polling GET http://someAdress/socket.io/?transport=polling&b64=1&sid=cGzUF60eLZLoF5fAAADA

When i place my emit with a completionHandler to get the responseData from the server i miss the ability to define the return type of the responseData.

socket.emit("request", with: ["Authenticate", param1, param2], completion: handleResponse)

func handleResponse()->(){
    debugPrint("Hello World!")
}

In TypeScript this works fine...

async call(command, param1, param2) {
   return new Promise((accept, reject) => {
     this.socket.emit('request', command, param1, param2, accept);
   });

Do you know of any way, how i can set a callback function and get the data that is placed in this callback like it is done in TypeScript.

Thanks in advance for your answers!

0 Answers
Related