Sha256: c7b4924521e3c205a3f245da203a71142e3f5d11f57c98798fe51409d9d00421
Contents?: true
Size: 1.99 KB
Versions: 3
Compression:
Stored size: 1.99 KB
Contents
require 'gserver' require 'json' module XRefreshServer # server class Server < GServer attr_accessor :clients def initialize(port, host, max_connections, *args) super @clients = Set.new @last_client_id = 0 OUT.puts "#{green('Started server')} on #{blue("#{host}:#{port}")} (max #{max_connections} clients)" end def serve(socket) socket.binmode @last_client_id += 1 client = Client.new(@last_client_id, socket) @clients.add(client) buffer = "" loop do # accumulate incomming input in @buffer begin buffer += socket.gets rescue break end begin # try to parse buffer msg = JSON.parse buffer rescue # buffer may be incomplete due to packet fragmentation ... else # got whole message => process it buffer = "" process(client, msg) end end end def process(client, msg) # see windows implementation in http://github.com/darwin/xrefresh/tree/master/src/winmonitor/Server.cs#ProcessMessage case msg["command"] when "Hello" client.type = msg["type"] || '?' client.agent = msg["agent"] || '?' OUT.puts "Client #{client.name} [#{magenta(client.agent)}] has connected" client.send_about(VERSION, AGENT) when "Bye" @clients.delete(client) OUT.puts "Client #{client.name} has disconnected" when "SetPage" url = msg["url"] || '?' OUT.puts "Client #{client.name} changed page to #{blue(url)}" end end end end
Version data entries
3 entries across 3 versions & 2 rubygems
Version | Path |
---|---|
darwin-xrefresh-server-0.1.5 | lib/xrefresh-server/server.rb |
xrefresh-server-0.1.6 | lib/xrefresh-server/server.rb |
xrefresh-server-0.1.7 | lib/xrefresh-server/server.rb |