diff options
Diffstat (limited to 'vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples')
4 files changed, 124 insertions, 0 deletions
diff --git a/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/echo.rb b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/echo.rb new file mode 100644 index 0000000..4e64886 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/echo.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../lib/em-websocket', __FILE__) + +EM.run { + EM::WebSocket.run(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws| + ws.onopen { |handshake| + puts "WebSocket opened #{{ + :path => handshake.path, + :query => handshake.query, + :origin => handshake.origin, + }}" + + ws.send "Hello Client!" + } + ws.onmessage { |msg| + ws.send "Pong: #{msg}" + } + ws.onclose { + puts "WebSocket closed" + } + ws.onerror { |e| + puts "Error: #{e.message}" + } + end +} 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" +} diff --git a/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/ping.rb b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/ping.rb new file mode 100644 index 0000000..fe569c3 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/ping.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../lib/em-websocket', __FILE__) + +EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws| + timer = nil + ws.onopen { + puts "Ping supported: #{ws.pingable?}" + timer = EM.add_periodic_timer(1) { + p ["Sent ping", ws.ping('hello')] + } + } + ws.onpong { |value| + puts "Received pong: #{value}" + } + ws.onping { |value| + puts "Received ping: #{value}" + } + ws.onclose { + EM.cancel_timer(timer) + puts "WebSocket closed" + } + ws.onerror { |e| + puts "Error: #{e.message}" + } +end diff --git a/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/test.html b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/test.html new file mode 100644 index 0000000..07d4636 --- /dev/null +++ b/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/test.html @@ -0,0 +1,29 @@ +<html> + <head> + <script> + function init() { + function debug(string) { + var element = document.getElementById("debug"); + var p = document.createElement("p"); + p.appendChild(document.createTextNode(string)); + element.appendChild(p); + } + + var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket; + var ws = new Socket("ws://localhost:8080/foo/bar?hello=world"); + ws.onmessage = function(evt) { debug("Received: " + evt.data); }; + ws.onclose = function(event) { + debug("Closed - code: " + event.code + ", reason: " + event.reason + ", wasClean: " + event.wasClean); + }; + ws.onopen = function() { + debug("connected..."); + ws.send("hello server"); + ws.send("hello again"); + }; + }; + </script> + </head> + <body onload="init();"> + <div id="debug"></div> + </body> +</html> |
