Class: Codenjoy::Client::Game

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

Instance Method Summary collapse

Instance Method Details

#play(url, log_level = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/codenjoy/client.rb', line 22

def play(url, log_level = nil)
  @log_level = log_level
  EM.run {
    ws = Faye::WebSocket::Client.new(ws_url(url))

    ws.on :open do |event|
      p [:open] if 'debug' == @log_level
    end

    ws.on :message do |event|
      yield ws, event.data
    end

    ws.on :close do |event|
      p [:close, event.code, event.reason]  if 'debug' == @log_level
      ws = nil
    end
  }
end

#source_pathObject



42
43
44
# File 'lib/codenjoy/client.rb', line 42

def source_path
  __dir__
end

#ws_url(url) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/codenjoy/client.rb', line 9

def ws_url(url)
  res = url
  res["board/player/"] = "ws?user="
  res["?code="] = "&code="

  if res.include?("https")
    res["https"] = "wss"
  else
    res["http"] = "ws"
  end
  res
end