Sha256: 00aadf168c2dd55dde7f99a5233663559eedc25c54f7433d582b5b84e44cb4d8

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'rubygems'
require 'socket'
require 'libwebsocket'

module PusherClient
  class WebSocket

    def initialize(url, params = {})
      @hs ||= LibWebSocket::OpeningHandshake::Client.new(:url => url, :version => params[:version])
      @frame ||= LibWebSocket::Frame.new

      @socket = TCPSocket.new(@hs.url.host, @hs.url.port || 80)

      @socket.write(@hs.to_s)
      @socket.flush

      loop do
        data = @socket.getc
        next if data.nil?

        result = @hs.parse(data.chr)

        raise @hs.error unless result

        if @hs.done?
          @handshaked = true
          break
        end
      end
    end

    def send(data)
      raise "no handshake!" unless @handshaked

      data = @frame.new(data).to_s
      @socket.write data
      @socket.flush
    end

    def receive
      raise "no handshake!" unless @handshaked

      data = @socket.gets("\xff")
      @frame.append(data)

      messages = []
      while message = @frame.next
        messages << message
      end
      messages
    end

    def socket
      @socket
    end

    def close
      @socket.close
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pusher-client-nc-0.2.1 lib/pusher-client/websocket.rb