Compare commits

...

2 Commits

Author SHA1 Message Date
Yessiest 250254d2df Added ability to IO.select websockets 2024-07-08 18:26:45 +04:00
Yessiest 582b0c1341 further fixes for websockets 2024-07-08 17:07:50 +04:00
2 changed files with 10 additions and 22 deletions

View File

@ -13,6 +13,7 @@ class Test < Landline::App
puts "Client closed read connection" puts "Client closed read connection"
end end
socket.write("Hi!") socket.write("Hi!")
puts IO.select([socket]).inspect
while (response = socket.read) while (response = socket.read)
if response if response
puts "Client wrote: #{response.inspect}" puts "Client wrote: #{response.inspect}"

View File

@ -24,28 +24,6 @@ module Landline
@__listeners[event]&.delete(listener) @__listeners[event]&.delete(listener)
end end
# Await for an event
# @param event [Symbol, Array<Symbol>] event or array of events to wait for
# @return [Array]
# @sg-ignore
def await(event)
blocking = true
output = nil
listener = proc do |*data|
output = data
blocking = false
end
if event.is_a? Array
event.each { |x| on(x, &listener) }
else
on(event, &listener)
end
while blocking; end
return output[0] if output.is_a? Array and output.length == 1
output
end
private private
# Trigger the queue clearing process # Trigger the queue clearing process
@ -115,6 +93,7 @@ module Landline
rescue Errno::EPIPE => e rescue Errno::EPIPE => e
_emit :error, e _emit :error, e
close close
nil
end end
# Read data from socket synchronously # Read data from socket synchronously
@ -192,6 +171,12 @@ module Landline
close_write if @writable close_write if @writable
end end
# Obtain internal IO object
# @return [IO]
def to_io
io
end
attr_reader :io, :readable, :writable attr_reader :io, :readable, :writable
private private
@ -225,6 +210,7 @@ module Landline
rescue Errno::ECONNRESET => e rescue Errno::ECONNRESET => e
_emit :error, e _emit :error, e
close close
nil
end end
# Receive data through websocket asynchronously # Receive data through websocket asynchronously
@ -238,6 +224,7 @@ module Landline
rescue Errno::ECONNRESET => e rescue Errno::ECONNRESET => e
_emit :error, e _emit :error, e
close close
nil
end end
end end
end end