Guest User

Untitled

a guest
Aug 19th, 2015
568
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // ViewController.swift
  3. // ChatRoom_ios
  4. //
  5. // Created by Minsu Kim on 8/13/15.
  6. // Copyright © 2015 Minsu Kim. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITableViewDataSource {
  12.  
  13. var messages: [String] = [String]()
  14. @IBOutlet weak var urlTextField: UITextField!
  15. @IBOutlet weak var messageTextField: UITextField!
  16.  
  17. @IBOutlet weak var messageTableView: UITableView!
  18.  
  19. var socket: SocketIOClient?
  20. @IBAction func connectButtonPressed(sender: UIButton) {
  21. print("Connect to Server!")
  22. print(urlTextField.text)
  23. if let url = urlTextField.text {
  24. print(url)
  25. socket = SocketIOClient(socketURL: url)
  26. }
  27.  
  28. socket?.connect()
  29. socket?.on("connect") { data, ack in
  30. print("iOS::WE ARE USING SOCKETS!")
  31. }
  32.  
  33. socket?.on("messageFromServer") {data, ack in
  34.  
  35. if let d = data {
  36. self.messages.append(d[0] as! String)
  37. }
  38.  
  39. self.messageTableView.reloadData()
  40. }
  41.  
  42. socket?.on("currentPlayers") {data, ack in
  43. print("List delivered!")
  44. if let d = data {
  45. print(d)
  46. }
  47. }
  48. }
  49.  
  50. @IBAction func sendButtonPressed(sender: UIButton) {
  51. socket?.emit("messageToServer", messageTextField.text!)
  52. }
  53.  
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. messageTableView.dataSource = self
  57. }
  58.  
  59. override func didReceiveMemoryWarning() {
  60. super.didReceiveMemoryWarning()
  61. }
  62.  
  63. // how many cells are we going to need?
  64. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  65. return messages.count
  66. }
  67. // what cell should I display for each row?
  68. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  69. // dequeue the cell from our storyboard
  70. let cell = tableView.dequeueReusableCellWithIdentifier("messageCell")!
  71.  
  72. // if the cell has a text label, set it to the model that is corresponding to the row in array
  73. cell.textLabel?.text = messages[indexPath.row]
  74.  
  75. // return cell so that Table View knows what to draw in each row
  76. return cell
  77. }
  78. }
RAW Paste Data