danielrhodes / Swift-ActionCableClient

ActionCable is a new WebSocket server being released with Rails 5 which makes it easy to add real-time features to your app. This Swift client makes it dead-simple to connect with that server, abstracting away everything except what you need to get going.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I can not get header info in server side

Ekct00 opened this issue · comments

commented

Rails side get nil

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    def connect
      p request.headers["Authorization"]
    end
  end
end

iOS side

 func setupClient() -> Void {
        self.client.headers = [
            "Authorization": "sometoken"
        ]
      
          self.client.willConnect = {
              print("Will Connect")
          }
          
          self.client.onConnected = {
              print("Connected to \(self.client.url)")
          }
          
          self.client.onDisconnected = {(error: ConnectionError?) in
            print("Disconected with error: \(String(describing: error))")
          }
          
          self.client.willReconnect = {
              print("Reconnecting to \(self.client.url)")
              return true
          }
          
          self.channel = client.create(ChatViewController.ChannelIdentifier)
          self.channel?.onSubscribed = {
              print("Subscribed to \(ChatViewController.ChannelIdentifier)")
          }
        
          self.channel?.onReceive = {(data: Any?, error: Error?) in
            if let error = error {
                print(error.localizedDescription)
                return
            }
            
            let JSONObject = JSON(data!)
            let msg = ChatMessage(name: JSONObject["name"].string!, message: JSONObject["message"].string!)
            self.history.append(msg)
            self.chatView?.tableView.reloadData()
            
            
            // Scroll to our new message!
            if (msg.name == self.name) {
                let indexPath = IndexPath(row: self.history.count - 1, section: 0)
                self.chatView?.tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
            }
        }
        
        self.client.connect()
    }