A
andreas.alin
Yow!
I'm trying to make a script that will act as a server (server 1), and
when a connection to the server server 1 is made, it will start a
thread that will connect to another server (server 2), and it will keep
the connection to server 2, even if the client disconnects.
The thing is that, as I've tried, my client/server will not connect to
another server, and if it does, the client can not disconnect.
Any ideas?
Thanks,
Andreas
----------------------
require 'socket'
class Session < Thread
def initalize(session_id)
super
@session_id = session_id
end
def session_id
@session_id
end
def send_message( message
# This method should send a message to
# server 2
puts message
end
end
threads = []
server = TCPServer.new("0.0.0.0", 12121)
while (session = server.accept)
session_id, message = session.gets.split(";", 2)
t = threads.map {|t| t if t["session_id"] == session_id }.delete_if
{|s| s == nil}.first
puts t
if t
puts "Found thread with existing session_id: #{ session_id }"
t.send :send_message, message
else
threads << Session.new(session) do |t|
Thread.current["session_id"] = session_id
puts "Creating thread for #{ session_id }"
end
end
session.close
end
I'm trying to make a script that will act as a server (server 1), and
when a connection to the server server 1 is made, it will start a
thread that will connect to another server (server 2), and it will keep
the connection to server 2, even if the client disconnects.
The thing is that, as I've tried, my client/server will not connect to
another server, and if it does, the client can not disconnect.
Any ideas?
Thanks,
Andreas
----------------------
require 'socket'
class Session < Thread
def initalize(session_id)
super
@session_id = session_id
end
def session_id
@session_id
end
def send_message( message
# This method should send a message to
# server 2
puts message
end
end
threads = []
server = TCPServer.new("0.0.0.0", 12121)
while (session = server.accept)
session_id, message = session.gets.split(";", 2)
t = threads.map {|t| t if t["session_id"] == session_id }.delete_if
{|s| s == nil}.first
puts t
if t
puts "Found thread with existing session_id: #{ session_id }"
t.send :send_message, message
else
threads << Session.new(session) do |t|
Thread.current["session_id"] = session_id
puts "Creating thread for #{ session_id }"
end
end
session.close
end