Class: XRefreshServer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xrefresh-server/client.rb

Overview

client representation on server side

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, socket) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/xrefresh-server/client.rb', line 9

def initialize(id, socket)
    @id = id
    @socket = socket
    @dead = false
    @type = '?'
    @agent = '?'
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



7
8
9
# File 'lib/xrefresh-server/client.rb', line 7

def agent
  @agent
end

#deadObject

Returns the value of attribute dead.



7
8
9
# File 'lib/xrefresh-server/client.rb', line 7

def dead
  @dead
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/xrefresh-server/client.rb', line 7

def id
  @id
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/xrefresh-server/client.rb', line 7

def type
  @type
end

Instance Method Details

#nameObject



17
18
19
# File 'lib/xrefresh-server/client.rb', line 17

def name
    green("#{@type}(#{@id})")
end

#send(data) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/xrefresh-server/client.rb', line 21

def send(data)
    return if @dead
    begin
        @socket << data.to_json + XREFRESH_MESSAGE_SEPARATOR
    rescue
        OUT.puts "Client #{name} #{red("is dead")}"
        @dead = true
    end
end

#send_about(version, agent) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/xrefresh-server/client.rb', line 31

def send_about(version, agent)
    send({
      "command" => "AboutMe", 
      "version" => version, 
      "agent" => agent
    })
end

#send_do_refresh(root, name, type, date, time, files) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/xrefresh-server/client.rb', line 39

def send_do_refresh(root, name, type, date, time, files)
    # read all CSS files and add them into response
    contents = {}
    files.each do |item|
        next unless item["path1"] =~ /\.css$/
        path = File.join(root, item["path1"]) 
        content = File.open(path).read
        contents[item["path1"]] = content
    end
    
    send({
      "command" => "DoRefresh", 
      "root" => root, 
      "name" => name, 
      "date" => date, 
      "time" => time, 
      "type" => type, 
      "files" => files,
      "contents" => contents
    })
end