Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

race condition can cause missed events #12

Closed
codekitchen opened this issue Jan 5, 2016 · 2 comments
Closed

race condition can cause missed events #12

codekitchen opened this issue Jan 5, 2016 · 2 comments

Comments

@codekitchen
Copy link

Because the Client currently spawns the socket reading thread in the object initializer, there's a small window where the user of this library hasn't had a chance to set up their event handlers yet, but the connection can emit events. For illustration:

ws = WebSocket::Client::Simple.connect 'ws://example.com:8888'

# a :message or :open event could be emitted in this window of time, before we
# have a chance to register the handler below, and it will be lost permanently

ws.on :open do |msg|
  puts msg.data
end

Possible solutions include:

# the connection thread is not actually started until this method is called, after we
# have a chance to register our handlers
ws.start

or:

ws = WebSocket::Client::Simple.connect('ws://example.com:8888') do |ws|
  # we have a chance to register our handlers during the constructor,
  # before the thread is started
  ws.on :open do |msg|
    puts msg.data
  end
end
shokai added a commit that referenced this issue Feb 19, 2016
shokai added a commit that referenced this issue Feb 20, 2016
shokai added a commit that referenced this issue Feb 20, 2016
shokai added a commit that referenced this issue Feb 20, 2016
  WebSocket::Client::Simple.connect "url" do |client|
    # ~~ do something ~~ #
  end

separate EchoServer in test
shokai added a commit that referenced this issue Feb 20, 2016
  WebSocket::Client::Simple.connect "url" do |client|
    # ~~ do something ~~ #
  end

separate EchoServer in test
@shokai
Copy link
Owner

shokai commented Feb 20, 2016

thank you for good suggestion.
I have implemented block-style. Now WebSocket::Client::Simple.connect runs a given block before connecting WebSocket.

shipped with v0.3.0

@shokai shokai closed this as completed Feb 20, 2016
@codekitchen
Copy link
Author

Awesome, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants