websocketd/src/instance_storage.cr

27 lines
820 B
Crystal
Raw Permalink Normal View History

2020-07-10 16:48:22 +02:00
# Link between fd and TCPSocket, WebSocket and IPC::Connection instances.
class InstanceStorage
property is_json : Hash(Int32, Bool)
property fd_to_websocket : Hash(Int32, WebSocket)
2020-07-11 16:08:46 +02:00
# So the GC won't close it.
property fd_to_socket : Hash(Int32, TCPSocket)
2020-07-10 16:48:22 +02:00
def initialize
@is_json = Hash(Int32,Bool).new
@fd_to_websocket = Hash(Int32, WebSocket).new
2020-07-11 16:08:46 +02:00
@fd_to_socket = Hash(Int32, TCPSocket).new
2020-07-10 16:48:22 +02:00
end
def remove_fd (fdclient : Int32)
puts "#{CBLUE}closing the client:#{CRESET} #{fdclient}"
# 1. remove the client and the service from is_json
@is_json.select! do |fd,v| fd != fdclient end
# 2. remove the client from fd_to_websocket
@fd_to_websocket.select! do |fd, ws| fd != fdclient end
2020-07-11 16:08:46 +02:00
# 3. remove the client from fd_to_socket
@fd_to_socket.select! do |fd, ws| fd != fdclient end
2020-07-10 16:48:22 +02:00
end
end