Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MeshRelay Action Cable Tests

Automated Message Sending

run in seperate terminals in this order:

  • mesh_relay_user_1.rb
  • mesh_relay_user_2.rb
    • sends a message to user 1
  • mesh_relay_user_3.rb
    • sends a message to user 1

Chat

These can be ran in any order after the action cable server is started

  • chat_user1.rb
  • chat_user2.rb

these both allow keyboard input, and will send messages to eachother. todo: why is event machine so slow in sending keyboard events?

# frozen_string_literal: true
require 'action_cable_client'
module KeyboardHandler
include EM::Protocols::LineText2
def receive_line(data)
@client.perform('chat', message: data, to: 'user2')
end
def client=(client)
@client = client
end
end
EventMachine.run do
client = ActionCableClient.new('ws://localhost:3001?uid=user1', 'MeshRelayChannel')
client.connected { puts 'successfully connected.' }
client.received do |message|
puts message
end
EM.open_keyboard(KeyboardHandler){ |kb| kb.client = client }
end
# frozen_string_literal: true
require 'action_cable_client'
module KeyboardHandler
include EM::Protocols::LineText2
def receive_line(data)
@client.perform('chat', message: data, to: 'user1')
end
def client=(client)
@client = client
end
end
EventMachine.run do
client = ActionCableClient.new('ws://localhost:3001?uid=user2', 'MeshRelayChannel')
client.connected { puts 'successfully connected.' }
client.received do |message|
puts message
end
EM.open_keyboard(KeyboardHandler){ |kb| kb.client = client }
end
# frozen_string_literal: true
require 'action_cable_client'
# this is just a runnable example from the readme
EventMachine.run do
client = ActionCableClient.new('ws://mesh-relay-in-us-1.herokuapp.com?user1', 'MeshRelayChannel')
client.connected { puts 'successfully connected.' }
client.received do |message|
puts '--------'
puts message
puts '--------'
end
end
# frozen_string_literal: true
require 'action_cable_client'
# this is just a runnable example from the readme
EventMachine.run do
client = ActionCableClient.new('ws://mesh-relay-in-us-1.herokuapp.com?uid=user2', 'MeshRelayChannel')
client.connected { puts 'successfully connected.' }
client.received do |message|
puts '--------'
puts message
puts '--------'
end
client.perform('chat', {
to: 'user1',
message: 'hello from user2'
})
end
# frozen_string_literal: true
require 'action_cable_client'
# this is just a runnable example from the readme
EventMachine.run do
client = ActionCableClient.new('ws://mesh-relay-in-us-1.herokuapp.com?uid=user3', 'MeshRelayChannel')
client.connected { puts 'successfully connected.' }
client.received do |message|
puts '--------'
puts message
puts '--------'
end
client.perform('chat', {
to: 'user1',
message: 'hello from user 3'
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment