Hi,
Please can someone tell me if this is even possible, I’ve been trying to
come up with various solutions and none of them seem to work.
I have a model which can send UDP packets, these need to be sent over a
specific port. Users using my rails application will be able to call
the model and send a UDP packet.
However, I also need to have a script running that listens for UDP
packets arriving on the same port. This script makes calls to the same
models as the Rails application and hence I am running the script via
script/runner. The script looks something like:
#listener script
$socket = UDPSocket.open
$socket.bind("",1810)
while true
udp_packet = $socket.recvfrom(512)
udp = UDP.new(udp_packet) #This is the same model as the
rails app uses
end
As I said, the UDP model needs to be able to send UDP packets over the
same socket as the script is listening on. i.e. the code looks like
class UDP
def send
$socket.send(“message”, 0, ip, port)
end
end
Hence, I need the an instance of socket to be bound to a particular port
and for it to be available to the Rails application AND the script
running via script/runner.
Is this even possible? I can’t think of a way to do it. I tried
looking into marshaling but it is not possible to marshal a socket.