Class: WebSocketServer
- Inherits:
-
Object
- Object
- WebSocketServer
- Defined in:
- lib/web/web-socket-ruby/lib/web_socket.rb
Instance Attribute Summary collapse
-
#accepted_domains ⇒ Object
readonly
Returns the value of attribute accepted_domains.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#tcp_server ⇒ Object
readonly
Returns the value of attribute tcp_server.
Instance Method Summary collapse
- #accept ⇒ Object
- #accepted_origin?(origin) ⇒ Boolean
- #create_web_socket(socket) ⇒ Object
-
#initialize(params_or_uri, params = nil) ⇒ WebSocketServer
constructor
A new instance of WebSocketServer.
- #origin_to_domain(origin) ⇒ Object
- #quit ⇒ Object
- #run(&block) ⇒ Object
Constructor Details
#initialize(params_or_uri, params = nil) ⇒ WebSocketServer
Returns a new instance of WebSocketServer.
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 452 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_domains ⇒ Object (readonly)
Returns the value of attribute accepted_domains.
472 473 474 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 472 def accepted_domains @accepted_domains end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
472 473 474 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 472 def port @port end |
#tcp_server ⇒ Object (readonly)
Returns the value of attribute tcp_server.
472 473 474 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 472 def tcp_server @tcp_server end |
Instance Method Details
#accept ⇒ Object
497 498 499 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 497 def accept() return @tcp_server.accept() end |
#accepted_origin?(origin) ⇒ Boolean
501 502 503 504 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 501 def accepted_origin?(origin) domain = origin_to_domain(origin) return @accepted_domains.any?(){ |d| File.fnmatch(d, domain) } end |
#create_web_socket(socket) ⇒ Object
514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 514 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
506 507 508 509 510 511 512 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 506 def origin_to_domain(origin) if origin == "null" || origin == "file://" # local file return "null" else return URI.parse(origin).host end end |
#quit ⇒ Object
493 494 495 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 493 def quit @run_threads.map(&:kill) end |
#run(&block) ⇒ Object
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/web/web-socket-ruby/lib/web_socket.rb', line 474 def run(&block) @run_threads = [] loop do @run_threads << 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 |