class Orator::Server
Handles the server connections and passing stuff to the event handler.
Attributes
clients[R]
The list of clients that are connected.
@return [Set<Client>]
event_handler[R]
The event handler that is duplicated for every client. This is to reduce the overhead of reinitializing the event handler for every client.
@return [EventHandler]
options[R]
The options defined for the server.
@return [Hash]
Public Class Methods
new(options)
click to toggle source
Initialize the server.
@return [void]
# File lib/orator/server.rb, line 25 def initialize(options) @options = options @clients = Set.new end
Public Instance Methods
run(&block)
click to toggle source
Runs the server with the given options.
# File lib/orator/server.rb, line 31 def run(&block) @event_handler = EventHandler.new(&block) EM::WebSocket.start(@options, &method(:handle_socket)) end
Private Instance Methods
handle_socket(socket)
click to toggle source
Handles setting up the socket. This binds the [#onopen], [#onclose],
- onerror], and [#onmessage
-
events.
@param [Object]
# File lib/orator/server.rb, line 42 def handle_socket(socket) client = Client.new :event_handler => @event_handler.dup, :socket => socket, :context => nil client.context = MiddleGround.new(client, clients) clients << client socket.onopen do |handshake| client.trigger 'socket.open', handshake end socket.onerror do |error| client.trigger 'socket.error', error end socket.onclose do |message| client.trigger 'socket.close', message clients.delete client end socket.onmessage do |data| json_data = Oj.load data, :mode => :null client.trigger json_data["event"], json_data end end