Newer
Older
intro2018 / sarver.rb
require 'em-websocket'


EM.run {
  @channel = EM::Channel.new
  EM::WebSocket.start(:host => "0.0.0.0", :port => 4946) do |ws|
    sid = nil
    ws.onopen { sid = @channel.subscribe { |msg| ws.send msg } }
    ws.onmessage { |msg| @channel.push "#{sid}: #{msg}" }

    ws.onclose { @channel.unsubscribe(sid) }
  end
}