Class: Websocket::Client
- Inherits:
-
Object
- Object
- Websocket::Client
- Defined in:
- lib/edi/websocket/client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#id ⇒ Object
Returns the value of attribute id.
-
#ws_url ⇒ Object
Returns the value of attribute ws_url.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #send_message(message, channel_name: "general", channel_id: nil) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
8 9 10 |
# File 'lib/edi/websocket/client.rb', line 8 def initialize self.id = 1 end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
6 7 8 |
# File 'lib/edi/websocket/client.rb', line 6 def client @client end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/edi/websocket/client.rb', line 6 def id @id end |
#ws_url ⇒ Object
Returns the value of attribute ws_url.
6 7 8 |
# File 'lib/edi/websocket/client.rb', line 6 def ws_url @ws_url end |
Instance Method Details
#connect ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/edi/websocket/client.rb', line 12 def connect connection = EDI.get("https://slack.com/api/rtm.start?token=#{EDI.bot_token}").response if connection["channels"] connection["channels"].each do |c| EDI.add_channel Slack::Channel.new(name: c["name"], id: c["id"]) end end self.ws_url = connection["url"] EM.run { self.client = Faye::WebSocket::Client.new(ws_url) client.on :open do |event| EDI::Logger.info "EDI is now online" end # Respond to Messages client.on :message do |event| = Slack::WebsocketIncomingMessage.new(event.data) if .should_respond? EDI::Logger.info "EDI received message #{.text} in channel #{.channel}" response_text = "" service = Proc.new { response_text = EDI.runner.new(message: ).execute } response = Proc.new { client.send Slack::WebsocketOutgoingMessage.new(text: response_text, channel: .channel, id: id).to_json if response_text } EM.defer service, response increment_id end end # Kepp Websocket Connection Alive EM.add_periodic_timer(1) do client.ping end } end |
#send_message(message, channel_name: "general", channel_id: nil) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/edi/websocket/client.rb', line 48 def (, channel_name: "general", channel_id: nil) unless channel_id channel_id = lookup_channel_by_name(channel_name).id end client.send Slack::WebsocketOutgoingMessage.new(text: , channel: channel_id, id: id).to_json increment_id end |