Created
November 28, 2013 03:50
-
-
Save tompurl/7687021 to your computer and use it in GitHub Desktop.
This is my first stab at a simple socket client to go against my cowsay server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
module CowSay | |
class Client | |
class << self | |
attr_accessor :host, :port | |
end | |
# Convert our arguments into a document that we can send to the cowsay | |
#>server. | |
# | |
# Options: | |
# message: The message that you want the cow to say | |
# body: The cowsay body that you want to use | |
def self.say(options) | |
if !options[:message] | |
raise "ERROR: Missing message argument" | |
end | |
if !options[:body] | |
options[:body] = "default" | |
end | |
request <<EOF | |
MESSAGE #{options[:message]} | |
BODY #{options[:body]} | |
EOF | |
end | |
def self.request(string) | |
# Create a new connection for each operation | |
@client = TCPSocket.new(host, port) | |
@client.write(string) | |
# Send EOF after writing the request | |
@client.close_write | |
# Read until EOF to get the response | |
@client.read | |
end | |
end | |
end | |
CowSay::Client.host = 'localhost' | |
CowSay::Client.port = 4481 | |
puts CowSay::Client.say message: 'this is cool!' | |
puts CowSay::Client.say message: 'This SUCKS!', body: 'beavis.zen' | |
puts CowSay::Client.say message: 'Moshi moshi!', body: 'hellokitty' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment