Created
May 10, 2016 11:44
-
-
Save nasitra/847a723552f4973cf5c491b206b1fd85 to your computer and use it in GitHub Desktop.
Broadcast server implemented with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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