diff options
| author | Benjamin Sanders <ben@Benjamins-MacBook-Pro.local> | 2025-10-11 10:34:24 -0400 |
|---|---|---|
| committer | Benjamin Sanders <ben@Benjamins-MacBook-Pro.local> | 2025-10-11 10:34:24 -0400 |
| commit | 5571dc766e2143e39762ff0b47c41d1c2e154f4c (patch) | |
| tree | f4f124d314a7e76c04484515aa0fd1f2e271a601 /vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/multicast.rb | |
| parent | 359f3309c97fc894b63e0a295c76ab298504d635 (diff) | |
Vendor bundled gems for deployment
Diffstat (limited to 'vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/multicast.rb')
| -rw-r--r-- | vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/multicast.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/multicast.rb b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/multicast.rb new file mode 100644 index 0000000..b1692d5 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/multicast.rb @@ -0,0 +1,47 @@ +require 'em-websocket' +# requires the twitter-stream gem +require 'twitter/json_stream' +require 'json' + +# +# broadcast all ruby related tweets to all connected users! +# + +username = ARGV.shift +password = ARGV.shift +raise "need username and password" if !username or !password + +EventMachine.run { + @channel = EM::Channel.new + + @twitter = Twitter::JSONStream.connect( + :path => '/1/statuses/filter.json?track=ruby', + :auth => "#{username}:#{password}", + :ssl => true + ) + + @twitter.each_item do |status| + status = JSON.parse(status) + @channel.push "#{status['user']['screen_name']}: #{status['text']}" + end + + + EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws| + + ws.onopen { + sid = @channel.subscribe { |msg| ws.send msg } + @channel.push "#{sid} connected!" + + ws.onmessage { |msg| + @channel.push "<#{sid}>: #{msg}" + } + + ws.onclose { + @channel.unsubscribe(sid) + } + } + + end + + puts "Server started" +} |
