imanel / websocket-eventmachine-client

WebSocket client for Ruby
74 stars 18 forks source link

Disconnected with status code - 1002 #16

Open selvaraman opened 6 years ago

selvaraman commented 6 years ago

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?

imanel commented 6 years ago

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.

selvaraman commented 6 years ago

Yes, It is working in the same network. But wscat is working If I run client/server in different network.

imanel commented 6 years ago

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.

faisalsahil commented 6 years ago

Getting same error. Any help?

imanel commented 6 years ago

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.

AlexisEvo commented 6 years ago

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.

matti commented 5 years ago

same here, tried doing simplest hello world

gathuku commented 4 years ago

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

matti commented 4 years ago

use ws-cat to test that your server is okay

On 4. Sep 2020, at 13.13, Moses Gathuku notifications@github.com 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.

gathuku commented 4 years ago

@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
matti commented 4 years ago

I guess you dont have a server running then. Sorry, can't help more.

On 4. Sep 2020, at 14.08, Moses Gathuku notifications@github.com 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.

imanel commented 4 years ago

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.

gathuku commented 4 years ago

@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:-

  1. 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.
  2. Rails comes with action-cable a websocket implementation, will it affect this gem implementation?

Thankyou.

imanel commented 4 years ago
  1. There is no clear convention about where to put your websocket-related code. You can do it either in app or lib directory, depending on your gut feeling and team convention. It should not interfere with Rails app in any way.
  2. 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?
gathuku commented 4 years ago

@imanel Thankyou, am not planning to use action cable at all. I give this a try.