This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user