Sha256: 2a414d532932c686ccfae2a02718be7d5d4445c8fbf7baf7040cf0f7f8c42f43

Contents?: true

Size: 969 Bytes

Versions: 1

Compression:

Stored size: 969 Bytes

Contents

require "undead/errors"
require "undead/web_socket_server"

module Undead
  class Server
    attr_reader :socket, :fixed_port, :timeout

    def initialize(fixed_port = nil, timeout = nil)
      @fixed_port = fixed_port
      @timeout    = timeout
      start
    end

    def port
      @socket.port
    end

    def timeout=(sec)
      @timeout = @socket.timeout = sec
    end

    def start
      @socket = Undead::WebSocketServer.new(fixed_port, timeout)
    end

    def stop
      @socket.close
    end

    def restart
      stop
      start
    end

    def send(command)
      receive_timeout = nil # default
      if command.name == 'visit'
        command.args.push(timeout) # set the client set visit timeout parameter
        receive_timeout = timeout + 5 # Add a couple of seconds to let the client timeout first
      end
      @socket.send(command.id, command.message, receive_timeout) or raise Undead::DeadClient.new(command.message)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
undead-0.2.0 lib/undead/server.rb