📜 ⬆️ ⬇️

Get websocket data using swift and populate UITableView (node.js server)

I did not find any actual guide on working with websocket in Xcode, here is my experience, I hope some newbie will help, and he will not spend so much time searching like me.

To connect via websocket we will use SocketIOClientSwift

You can download it here .
')
The installation there is also described in principle, but for those who are completely in the tank I will tell you that there is an easy way, just add the files from the source folder to your project.

In our example, we will receive tickets from the technical support system that runs on node.js

Create an array of our tickets (empty for now):

var arrayOfTickets:[String] = [] 

You can make data on the button or when loading, in our example when loading.

  override func viewDidLoad() { super.viewDidLoad() let hostUrl = NSURL(string:"http://IP__:")! //    let tokenSDK = "  " //    let socket = SocketIOClient(socketURL: hostUrl, options: ["log": false, "reconnects": true, "reconnectAttempts": 1, "reconnectWait": 1, "connectParams": ["token":tokenSDK]]) //      socket.on("connect") {data,ack in let hejka = [""] //   ( ) let paginav = [""] //   2 ( ) socket.emitWithAck("_", "", hejka, paginav)(timeoutAfter: 0) //     {data in let dataTickets = data[1]["result"] as! NSArray //  json   let howMuchTickets = dataTickets.valueForKey("name") for (var i=0; i < howMuchTickets.count; i++){ let ticketName = dataTickets[i].valueForKey("name") as? String self.arrayOfTickets.append(ticketName!) //    } } } socket.connect() 

We count how many cells we need to generate in the table:

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayOfTickets.count } 

We display data (we have custom cells in the example, how to make them is not the essence of my post, but if you have questions, write):

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // dequeue the cell from our storyboard let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell // if the cell has a text label, set it to the model that is corresponding to the row in array cell.ticketName?.text = arrayOfTickets[indexPath.row] // return cell so that Table View knows what to draw in each row return cell } 

Now for the most attentive, as we know the request goes asynchronous, and when executing this code, nothing will appear.

In order for us to appear, we need to call the tableview function, and tell it to reload the data, that is, add one line.

  override func viewDidLoad() { super.viewDidLoad() let hostUrl = NSURL(string:"http://IP__:")! //    let tokenSDK = "  " //    let socket = SocketIOClient(socketURL: hostUrl, options: ["log": false, "reconnects": true, "reconnectAttempts": 1, "reconnectWait": 1, "connectParams": ["token":tokenSDK]]) //      socket.on("connect") {data,ack in let hejka = [""] //   ( ) let paginav = [""] //   2 ( ) socket.emitWithAck("_", "", hejka, paginav)(timeoutAfter: 0) //     {data in let dataTickets = data[1]["result"] as! NSArray let howMuchTickets = dataTickets.valueForKey("name") for (var i=0; i < howMuchTickets.count; i++){ let ticketName = dataTickets[i].valueForKey("name") as? String self.arrayOfTickets.append(ticketName!) // //    self.tableView.reloadData() //    } } } socket.connect() 

I hope the guide will be useful if you have questions or you know how to make better / faster / more beautiful write.

Source: https://habr.com/ru/post/277635/


All Articles