-
Notifications
You must be signed in to change notification settings - Fork 19
Open
Description
I am running my web socket server in aws and client in local machine. Here is my server and client code.
Server:
%w(rubygems erubis eventmachine em-websocket sqlite3 active_record json pry).each{|lib|require lib}
EM.kqueue = true if EM.kqueue?
EM.epoll = true if EM.epoll?
ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: 'development.sqlite3', timeout: 1000)
class Sample < ActiveRecord::Base
end
class Record < ActiveRecord::Base
end
EventMachine.run {
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8086) do |ws|
$_ws_ = ws
module Handler
def file_modified
records = Record.where(done: false).select(:id, :job, :entity, :value)
records.each do |record|
$_ws_.send(record.to_json)
end
end
end
EM.watch_file('development.sqlite3', Handler)
ws.onopen {
puts "WebSocket connection open"
}
ws.onmessage do |msg|
puts "WebSocket Recieved message: #{msg}"
res = JSON.parse(msg)
if res.kind_of?(Hash)
id = res['id']
record = Record.where(id: id).first
if record
record.update_attributes(res)
else
$_ws_.send('Invalid id received')
end
else
$_ws_.send("<Invalid msg received>")
end
end
ws.onclose { puts "WebSocket Connection closed" }
end
}
Client:
require 'websocket-eventmachine-client'
require 'json'
EM.run do
#ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://localhost:8086')
ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://18.217.243.152:8086')
ws.onopen do
puts "Connected"
end
ws.onmessage do |msg, type|
puts "Received message: #{msg}"
res = JSON.parse(msg)
if res.kind_of?(Hash)
res['done'] = true
ws.send(res.to_json)
else
ws.send('<Invalid data received> ' + msg )
end
end
ws.onerror do |err|
puts "Err: #{err}"
end
ws.onclose do |code|
puts "Disconnected with status code: #{code}"
end
EventMachine.next_tick do
ws.send "Hello Server!"
end
end
Both are working properly when I run local machine. But running the server in aws and the client in local machine, client reports status 1002. But with wscat It is working properly. What is wrong in my client code?
Metadata
Metadata
Assignees
Labels
No labels