开发者

Sending socket message with ruby without closing connection

开发者 https://www.devze.com 2023-03-25 11:45 出处:网络
I\'m working on a small ruby client that sends a continuous stream of information to a socket server (which is then passed on to other clients listening). I do not want to have to close the connection

I'm working on a small ruby client that sends a continuous stream of information to a socket server (which is then passed on to other clients listening). I do not want to have to close the connection every time data is sent, however it seems that with ruby, the data is not sent until I close the connection. Is there another way to do this without reconnecting to the server? It is essential that the outgoing data is passed along as soon as it is sent by the client script.

Currently I have to do this within a loop:

s = TCPsocket.new('127.0.0.1',2000)
s.send('Hello World',0)
s.close

However, that means that I am constantly having to reconnect to the socket server, often in very quick succes开发者_JAVA百科sion. I would like to be able to connect to the socket server outside of the loop and close when the loop is done. If I do that now, the send data will all be sent at once when the 'close' is initiated.

Is it possible to keep the connection open while continuing to send information? I know this is possible in other languages and scripts as I have done it several times in AS3.

Thanks in advance.


To answer my own question (hopefully to the help of others), appending \000 after the message in the ruby socket.send('some message\000', 0) works. To clarify, you can type this

socket = TCPsocket.new('127.0.0.1',2000)
socket.send('some message\000', 0)

Above will send a message without first to close the socket connection first (socket.close). Also for reference is the ruby socket server, which I am using to broadcast to Flash.

require 'socket'
portnumber = 2000
socketServer = TCPServer.open(portnumber)

while true
  Thread.new(socketServer.accept) do |connection|
    puts "Accepting connection from: #{connection.peeraddr[2]}"

    begin
      while connection
        incomingData = connection.gets("\0")
        if incomingData != nil
          incomingData = incomingData.chomp
        end

        puts "Incoming: #{incomingData}"

        if incomingData == "DISCONNECT\0"
          puts "Received: DISCONNECT, closed connection"
          connection.close
          break
        else
          connection.puts "#{incomingData}"
          connection.flush
        end
      end
    rescue Exception => e
      # Displays Error Message
      puts "#{ e } (#{ e.class })"
    ensure
      connection.close
      puts "ensure: Closing"
    end
  end
end

I should give credit where due and the socket server code is based on this blog post: http://www.giantflyingsaucer.com/blog/?p=1147


use close_write() to disallow further write using shutdown system call. More Socket, BasicSocket

Socket.tcp("www.ruby-lang.org", 80) {|sock|
  sock.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
  sock.close_write
  puts sock.read
}


Does flush work?

s = TCPSocket.new('127.0.0.1', 2000)
1.upto(10) do |n|
  s.send("Hello #{n}\n", 0)
  s.flush
end
s.close
0

精彩评论

暂无评论...
验证码 换一张
取 消