Skip to content

Instantly share code, notes, and snippets.

@nasitra
Created May 10, 2016 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasitra/847a723552f4973cf5c491b206b1fd85 to your computer and use it in GitHub Desktop.
Save nasitra/847a723552f4973cf5c491b206b1fd85 to your computer and use it in GitHub Desktop.
Broadcast server implemented with Ruby
require 'em-websocket'
require 'webrick'
EM.run {
@clients = []
EM::WebSocket.start(:host => '0.0.0.0', :port => '3001') do |ws|
ws.onopen do |handshake|
@clients.push ws
end
ws.onclose do
@clients.delete ws
end
ws.onmessage do |msg|
@clients.each do |socket|
socket.send msg
end
end
end
Thread.new do
WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => './').start
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment