Class: WebSocketServer

Inherits:
Object
  • Object
show all
Defined in:
lib/web_socket/web_socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params_or_uri, params = nil) ⇒ WebSocketServer

Returns a new instance of WebSocketServer.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/web_socket/web_socket.rb', line 298

def initialize(params_or_uri, params = nil)
  if params
    uri = params_or_uri.is_a?(String) ? URI.parse(params_or_uri) : params_or_uri
    params[:port] ||= uri.port
    params[:accepted_domains] ||= [uri.host]
  else
    params = params_or_uri
  end
  @port = params[:port] || 80
  @accepted_domains = params[:accepted_domains]
  if !@accepted_domains
    raise(ArgumentError, "params[:accepted_domains] is required")
  end
  if params[:host]
    @tcp_server = TCPServer.open(params[:host], @port)
  else
    @tcp_server = TCPServer.open(@port)
  end
end

Instance Attribute Details

#accepted_domainsObject (readonly)

Returns the value of attribute accepted_domains.



318
319
320
# File 'lib/web_socket/web_socket.rb', line 318

def accepted_domains
  @accepted_domains
end

#portObject (readonly)

Returns the value of attribute port.



318
319
320
# File 'lib/web_socket/web_socket.rb', line 318

def port
  @port
end

#tcp_serverObject (readonly)

Returns the value of attribute tcp_server.



318
319
320
# File 'lib/web_socket/web_socket.rb', line 318

def tcp_server
  @tcp_server
end

Instance Method Details

#acceptObject



338
339
340
# File 'lib/web_socket/web_socket.rb', line 338

def accept()
  return @tcp_server.accept()
end

#accepted_origin?(origin) ⇒ Boolean

Returns:

  • (Boolean)


342
343
344
345
# File 'lib/web_socket/web_socket.rb', line 342

def accepted_origin?(origin)
  domain = origin_to_domain(origin)
  return @accepted_domains.any?(){ |d| File.fnmatch(d, domain) }
end

#create_web_socket(socket) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
# File 'lib/web_socket/web_socket.rb', line 355

def create_web_socket(socket)
  ch = socket.getc()
  if ch == ?<
    # This is Flash socket policy file request, not an actual Web Socket connection.
    send_flash_socket_policy_file(socket)
    return nil
  else
    socket.ungetc(ch)
    return WebSocket.new(socket, :server => self)
  end
end

#origin_to_domain(origin) ⇒ Object



347
348
349
350
351
352
353
# File 'lib/web_socket/web_socket.rb', line 347

def origin_to_domain(origin)
  if origin == "null" || origin == "file://"  # local file
    return "null"
  else
    return URI.parse(origin).host
  end
end

#run(&block) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/web_socket/web_socket.rb', line 320

def run(&block)
  while true
    Thread.start(accept()) do |s|
      begin
        ws = create_web_socket(s)
        yield(ws) if ws
      rescue => ex
        print_backtrace(ex)
      ensure
        begin
          ws.close_socket() if ws
        rescue
        end
      end
    end
  end
end