Sha256: e488d051c21961c5d0b1908cf6a18c9304e54fdedda42b600407e80089a8007d
Contents?: true
Size: 1.58 KB
Versions: 1
Compression:
Stored size: 1.58 KB
Contents
# frozen_string_literal: true require "socket" require "forwardable" require "protocol/websocket/headers" require "protocol/websocket/framer" require "protocol/websocket/text_frame" require_relative "upgrade_request" require_relative "http_response" require_relative "response_exception" module Wands # This is a class that represents WebSocket, which has the same interface as TCPSocket. # # The WebSocket class is responsible for reading and writing messages to the server. # # Example usage: # # web_socket = WebSocket.open('localhost', 2345) # web_socket.write("Hello World!") # # puts web_socket.gets # # web_socket.close # class WebSocket include Protocol::WebSocket::Headers extend Forwardable def_delegators :@socket, :addr, :remote_address, :close, :to_io def self.open(host, port) socket = TCPSocket.new(host, port) request = UpgradeRequest.new(host, port) socket.write(request.to_s) socket.flush response = HTTPResponse.new response.parse(socket) raise ResponseException.new("Bad Status", response) unless response.status == "101" request.verify response new(socket) end def initialize(socket) @socket = socket end # @return [String] def gets framer = Protocol::WebSocket::Framer.new(@socket) frame = framer.read_frame raise "frame is not a text" unless frame.is_a? Protocol::WebSocket::TextFrame frame.unpack end def write(message) frame = Protocol::WebSocket::TextFrame.new(true, message) frame.write(@socket) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
wands-0.1.0 | lib/wands/web_socket.rb |