Created
May 6, 2012 10:24
-
-
Save pochi/2621489 to your computer and use it in GitHub Desktop.
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
#!/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