Skip to content

Instantly share code, notes, and snippets.

@pochi
Created May 6, 2012 10:24
Show Gist options
  • Save pochi/2621489 to your computer and use it in GitHub Desktop.
Save pochi/2621489 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'libwebsocket'
module EchoServer
def post_init
puts "someone with websocket!"
end
def receive_data(data)
@hs ||= LibWebSocket::OpeningHandshake::Server.new
@frame ||= LibWebSocket::Frame.new
if [email protected]?
@hs.parse(data)
if @hs.done?
send_data(@hs.to_s)
end
return
end
puts data.inspect
@frame.append(data)
while message = @frame.next
send_data @frame.new(message).to_s
end
end
end
EventMachine::run do
host = '0.0.0.0'
port = 8080
EventMachine::start_server host, port, EchoServer
puts "Started EchoServer on #{host}:#{port}..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment