Sha256: 90e5bea1fe7e40732a8bea210e0857eb7474bf010375b4c7d03b7d7a9c525ecf

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

Faye::WebSocket.load_adapter('thin')

module Grenache
  class WebsocketServer
    def initialize(port, &block)
      @port = port
      @callback = block
    end

    def start_server
      @server = Thin::Server.start('0.0.0.0', @port, method(:app), {signals: false})
    end

    def app(env)
      ws = Faye::WebSocket.new(env)
      ws.on :message, -> (ev) do
        req = ServiceMessage.parse(ev.data)
        err, payload = @callback.call(req)
        ws.send(ServiceMessage.new(payload, err, req.rid).to_json)
      end
      ws.rack_response
    end

    def send(payload)
      @server.send(payload)
    end

    def connected?
      !! @ws
    end
  end

  class WebsocketClient
    def initialize(uri, &cb)
      @uri = uri.sub("tcp://","ws://")
      @uri.prepend("ws://") unless @uri.start_with?("ws://")
      @callback = cb
    end

    def send payload
      ws_connect unless @connected
      @ws.send(payload)
    end

    def connected?
      @connected
    end

    private
    def ws_connect
      @ws = Faye::WebSocket::Client.new(@uri)
      @ws.on(:open, method(:on_open))
      @ws.on(:message, method(:on_message))
      @ws.on(:close, method(:on_close))
    end

    def on_open(ev)
      @connected = true
    end

    def on_message(ev)
      msg = ServiceMessage.parse(ev.data)
      err = msg.err.kind_of?(Exception) ? msg.err.message : msg.err
      @callback.call(msg.err, msg.payload) if @callback
    end

    def disconnect
      @ws.close
      @ws = nil
    end

    def on_close(ev)
      @connected = false
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grenache-ruby-ws-0.2.13 lib/grenache/websocket.rb
grenache-ruby-ws-0.2.12 lib/grenache/websocket.rb