websocket-eventmachine-client
websocket-eventmachine-client copied to clipboard
Disconnected with status code - 1002
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?
Looks like everything should work as expected. Have you tried to run simple echo client/server in the same network configuration? Maybe there's problem on network level.
Yes, It is working in the same network. But wscat is working If I run client/server in different network.
What I meant was to try using simplest client/server possible (built using this client) while using different network. That would show if problem is happening in all cases, or only in more complex ones.
Getting same error. Any help?
Hi @faisalsahil. Could you provide some details about your network configuration so I can help you debug it? I went through code several times and didn't spotted any obvious causes.
I am getting this error regularly when connecting to a mining pool software goburstpool. Oddly, it seems to work fine using WSS with nginx 1.14 and 1.15 acting as an SSL terminator, but fails when connecting directly to the server (WS) or when reverse proxied over nginx (no SSL).
These servers can reliably reproduce the disconnect within 10 seconds of connecting:
ws://xen.poolofd32th.club/ws # nginx proxy ws://pool.poolofd32th.club/ws # nginx proxy ws://bibenwei.com:8081/ws # direct to server
This server works with nginx doing SSL termination, but fails when connecting directly to the pool:
wss://voiplanparty.com/ws # nginx ws://voiplanparty.com:8124/ws # direct to server
Reproduction against any of the above is relatively easy:
require 'websocket-eventmachine-client'
EM.run do
ws = WebSocket::EventMachine::Client.connect(:uri => "ws://voiplanparty.com:8124/ws")
ws.onopen do
puts "connected to server"
end
ws.onmessage do |msg, type|
puts msg
end
ws.onclose do |code, reason|
puts "disconnected, code: #{code}, reason: #{reason}"
end
end
I know relatively little about the websocket side. Any assistance would be greatly appreciated. If it's a bug in goburstpool's ws library I'm happy to liaison and fix it with them, but 1) it works in all major browsers, and 2) I haven't been able to track down the exact source of the disconnect.
same here, tried doing simplest hello world
I need some help ,
Am try to connect to a fingerprint Devise that use webSocket , I have locally connect the devise with my computer using ethernet and assigned the static ips, that's fine and am able to ping my fingerprint device.
I have this code in test.rb
file.
require 'websocket-eventmachine-client'
EM.run do
ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://192.168.1.224')
ws.onopen do
puts "Connected"
end
ws.onmessage do |msg, type|
puts "Received message: #{msg}"
end
ws.onclose do |code, reason|
puts "Disconnected with status code: #{code}"
end
#EventMachine.next_tick do
#ws.send "Hello Server!"
#end
end
When i run ruby test.rb
. Am getting Disconnected with status code: 1002
use ws-cat to test that your server is okay
On 4. Sep 2020, at 13.13, Moses Gathuku [email protected] wrote:
I need some help , Am try to connect to a fingerprint Devise that use webSocket , I have locally connect the devise with my computer using ethernet and assigned the static ips, that's fine and am able to ping my fingerprint device. I have this code in test.rb file.
EM.run do
ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://192.168.1.224')
ws.onopen do puts "Connected" end
ws.onmessage do |msg, type| puts "Received message: #{msg}" end
ws.onclose do |code, reason| puts "Disconnected with status code: #{code}" end
#EventMachine.next_tick do #ws.send "Hello Server!" #end
end When i run ruby test.rb. Am getting Disconnected with status code: 1002
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
@matti could you please explain how to setup the server ? Does this gem setup the websocket server or i will have to set myself?
$ wscat -c ws://192.168.1.224
error: socket hangs up
I guess you dont have a server running then. Sorry, can't help more.
On 4. Sep 2020, at 14.08, Moses Gathuku [email protected] wrote:
@matti could you please explain how to setup the server ? Does this gem setup the server or i will have to set myself?
$ wscat -c ws://192.168.1.224 error: socket hangs up — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.
This gem only connects to already existing servers. You can run one by yourself either by using https://github.com/imanel/websocket-eventmachine-server, or any other implementation. You can find basic example in examples folder in server repo, however if you will need additional support then please don't hesitate to ask.
@imanel Thankyou , That was helpful and was able to run the server and everything worked fine. Now i want to move the implementation to a rails app. I have some questions which i have not investigated:-
- How to run the this gem socket server parallel with rails webserver. Am not sure but Rails application code is loaded on demand. Where do i put the this gem code to ensure it's ever running as long as the web server is running.
- Rails comes with
action-cable
a websocket implementation, will it affect this gem implementation?
Thankyou.
- There is no clear convention about where to put your websocket-related code. You can do it either in
app
orlib
directory, depending on your gut feeling and team convention. It should not interfere with Rails app in any way. - As long as they will be running on different ports then no, not at all. The big question is if you want to have two websocket servers running in the first place? Is this a special case, or you don’t plan to use action cable at all?
@imanel Thankyou, am not planning to use action cable at all. I give this a try.