Reputation: 63
I search through the internet for a proper example for the use of Socket.IO
on iOS (actually th client side - socket.io-client-swift
) with clear coding examples. anyone can help me out with this.
Upvotes: 3
Views: 7244
Reputation: 1278
here an example of socket io client.
import SocketIO
class soket {
var lat = ""
var lng = ""
let appDelegate = UIApplication.shared.delegate as!AppDelegate
init(lat: String, lng: String) {
self.lat = lat
self.lng = lng
}
func connect() {
print("llamada al socket")
print(contantes.init().addres)
let socket = SocketIOClient(socketURL: URL(string: contantes.init().addres)!,config: [.connectParams(["accessToken" : appDelegate.token]),.forcePolling(true),.nsp("/vendedor"), .log(true)])
let myJSON = [
"lng":lng,
"lat":lat,
"idvendedor":appDelegate.idSeller
]
socket.on("connect") {data, ack in
print("socket connected")
socket.emit("setLocation",myJSON)
print("Mostrando el Json: \(myJSON)")
}
socket.on("locationChanged", callback: {_,_ in
print("disconnected")
socket.disconnect()
})
socket.connect()
}
}
this class is a simple socket to report device location to a server side, in this case the server side use namespace so the socket must join it so send data. here is th code how is called in viewController
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
let sok = soket(lat: String(location.coordinate.latitude),lng:String(location.coordinate.longitude))
sok.connect()
locationManager.stopUpdatingLocation()
}
}
the main concepts behind sockets is that configure handlers to listen or emit data from server and the make the connection. here another example Socket.IO client integration in iOS Swift
Upvotes: 1