Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // ChatRoom_ios
- //
- // Created by Minsu Kim on 8/13/15.
- // Copyright © 2015 Minsu Kim. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController, UITableViewDataSource {
- var messages: [String] = [String]()
- @IBOutlet weak var urlTextField: UITextField!
- @IBOutlet weak var messageTextField: UITextField!
- @IBOutlet weak var messageTableView: UITableView!
- var socket: SocketIOClient?
- @IBAction func connectButtonPressed(sender: UIButton) {
- print("Connect to Server!")
- print(urlTextField.text)
- if let url = urlTextField.text {
- print(url)
- socket = SocketIOClient(socketURL: url)
- }
- socket?.connect()
- socket?.on("connect") { data, ack in
- print("iOS::WE ARE USING SOCKETS!")
- }
- socket?.on("messageFromServer") {data, ack in
- if let d = data {
- self.messages.append(d[0] as! String)
- }
- self.messageTableView.reloadData()
- }
- socket?.on("currentPlayers") {data, ack in
- print("List delivered!")
- if let d = data {
- print(d)
- }
- }
- }
- @IBAction func sendButtonPressed(sender: UIButton) {
- socket?.emit("messageToServer", messageTextField.text!)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- messageTableView.dataSource = self
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- // how many cells are we going to need?
- func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return messages.count
- }
- // what cell should I display for each row?
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- // dequeue the cell from our storyboard
- let cell = tableView.dequeueReusableCellWithIdentifier("messageCell")!
- // if the cell has a text label, set it to the model that is corresponding to the row in array
- cell.textLabel?.text = messages[indexPath.row]
- // return cell so that Table View knows what to draw in each row
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment