Created
March 18, 2013 16:53
-
-
Save adamvduke/5188741 to your computer and use it in GitHub Desktop.
A quick mockup to demonstrate the socket API that we'd like to have for handling errors and/or broken connections for ZeroPush
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' | |
server = TCPServer.open(2195) | |
connection = server.accept | |
loop { | |
data = connection.read(8) | |
puts "Received \"#{data}\"" | |
if data == "CCCCCCCC" | |
connection.write("error") | |
connection.close | |
puts "closed connection, opening another" | |
connection = server.accept | |
end | |
} |
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' | |
class TCPSocket | |
def on_data | |
end | |
def on_close | |
end | |
end | |
def write_to_socket(socket, c) | |
puts "writing #{c}#{c}#{c}#{c}#{c}#{c}#{c}#{c}" | |
8.times do | |
socket.write(c) | |
end | |
sleep(1.0) | |
end | |
def create_socket | |
TCPSocket.open('localhost', 2195) | |
end | |
socket = create_socket | |
# it would be really awesome to have an on_data callback, in case data is written | |
# to the socket when we aren't expecting it | |
socket.on_data do |data| | |
puts "OMG something came in on the socket" | |
#read the data and act on it | |
end | |
# it would be really awesome to have an on_close callback, in case the socket | |
# is closed when we arent' exepecting it | |
socket.on_close do | |
puts "OMG the socket is closed" | |
socket = create_socket | |
end | |
write_to_socket(socket, "A") | |
write_to_socket(socket, "B") | |
# This should kill the socket, and it would be awesome if the on_data method | |
# got run, followed by the on_close | |
write_to_socket(socket, "C") | |
socket.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment