Last active
February 15, 2016 07:02
-
-
Save Asmod4n/241c84ef31df921b31e9 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
require 'celluloid/io' | |
require 'websocket/driver' | |
class Server | |
include Celluloid::Logger | |
include Celluloid::IO | |
def initialize | |
@server = TCPServer.new('127.0.0.1', 5009) | |
async.run | |
end | |
def run | |
loop { async.handle_connection @server.accept } | |
end | |
def handle_connection(socket) | |
driver = WebSocket::Driver.server(socket) | |
driver.on(:close) do |e| | |
debug "code: #{e.code}\nreason: #{e.reason}" | |
socket.close | |
end | |
driver.on(:message) { |e| info e.data } | |
driver.on(:connect) do | |
if WebSocket::Driver.websocket?(driver.env) | |
driver.start | |
else | |
driver.close | |
end | |
end | |
loop { driver.parse socket.readpartial(16384) } | |
rescue => e | |
crash "#{self.class} crashed!", e | |
socket.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment