Reputation: 22064
Using TCPSocket
, I need to socket.puts "foobar"
, and then socket.close
to let the socket in the other side socket.read
the message.
Is there a way to send or receive a message though a socket, but without closing the socket, which mean I can send message again without creating a socket again?
p.s Something like websocket
Upvotes: 0
Views: 1350
Reputation: 6961
If you traverse up the super class chain you will eventually see that you inherit from IO
. Most IO
objects, in Ruby, buffer the data to be more efficient writing and reading from disk.
In your case, the buffer wasn't large enough (or enough time didn't pass) for it to flush out. However, when you closed the socket, this forced a flush of the buffer resources.
You have a few options:
IO#flush
.IO#sync=
to true
. You can check the status of your IO
object's syncing using IO#sync
; I'm guessing you'd see socket.sync #=> false
BasicSocket#send
which will call POSIX send(2)
; since sockets are initialized with O_FSYNC
set, the send will be synchronous and atomic.Upvotes: 3
Reputation: 6310
It should not be neccessary to close the connection in order for the other party to read it. send
should transfer the data over the conection immediately. Make sure the other party is reading from the socket.
Upvotes: 1