Class: DRb::WebSocket::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/drb/websocket/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, config) ⇒ Server

Returns a new instance of Server.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/drb/websocket/server.rb', line 46

def initialize(uri, config)
  @uri = uri
  @config = config
  @queue = Thread::Queue.new

  Faye::WebSocket.load_adapter('thin')

  u = URI.parse(uri)
  RackApp.register(uri, self)

  if RackApp.config.standalone
    Thread.new do
      app = RackApp.new(-> { [400, {}, []] })
      thin = Rack::Handler.get('thin')
      thin.run(app, Host: u.host, Port: u.port)
    end.run
  end
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



44
45
46
# File 'lib/drb/websocket/server.rb', line 44

def uri
  @uri
end

Instance Method Details

#acceptObject



73
74
75
76
# File 'lib/drb/websocket/server.rb', line 73

def accept
  callback = @queue.pop
  ServerSide.new(callback, @config, uri)
end

#closeObject



65
66
67
# File 'lib/drb/websocket/server.rb', line 65

def close
  RackApp.close(@uri)
end

#on_message(data) ⇒ Object



78
79
80
81
82
# File 'lib/drb/websocket/server.rb', line 78

def on_message(data)
  callback = Callback.new(self)
  res = callback.recv_mesg(data.pack('C*'))
  @ws.send(res.bytes)
end

#on_session_start(ws) ⇒ Object



84
85
86
# File 'lib/drb/websocket/server.rb', line 84

def on_session_start(ws)
  @ws = ws
end

#push(callback) ⇒ Object



69
70
71
# File 'lib/drb/websocket/server.rb', line 69

def push(callback)
  @queue.push(callback)
end