Class: WebSocketServer

Inherits:
Object
  • Object
show all
Defined in:
lib/em-ws-request/vendor/web-socket-ruby/lib/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.


444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 444

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


464
465
466
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 464

def accepted_domains
  @accepted_domains
end

#portObject (readonly)

Returns the value of attribute port


464
465
466
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 464

def port
  @port
end

#tcp_serverObject (readonly)

Returns the value of attribute tcp_server


464
465
466
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 464

def tcp_server
  @tcp_server
end

Instance Method Details

#acceptObject


484
485
486
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 484

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

#accepted_origin?(origin) ⇒ Boolean

Returns:

  • (Boolean)

488
489
490
491
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 488

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

#create_web_socket(socket) ⇒ Object


501
502
503
504
505
506
507
508
509
510
511
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 501

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) if ch
    return WebSocket.new(socket, :server => self)
  end
end

#origin_to_domain(origin) ⇒ Object


493
494
495
496
497
498
499
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 493

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


466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/em-ws-request/vendor/web-socket-ruby/lib/web_socket.rb', line 466

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