Ruby中两个客户端之间的通信

时间:2016-10-13 01:22:01

标签: ruby sockets client-server chat

我正在尝试创建一个Ruby程序,它将在两个客户端之间发送消息,服务器位于中间。例如,客户端A连接到服务器,客户端B连接到同一服务器和端口。客户A说“你好吗?”和客户B说“做得好”。有点像聊天服务器。我正在考虑让服务器从客户端A获取消息并发送到客户端B.到目前为止,我有一个使用证书连接的工作客户端和服务器程序。我遇到的问题是我的服务器发送回客户端A,我不知道如何发送到第二个连接(客户端B)。

客户端

#!/usr/bin/ruby
require "socket"
require "openssl"
require "thread"

host = ARGV[0]
port = Integer(ARGV[1])

socket = TCPSocket.new(host, port)
certificate = OpenSSL::X509::Certificate.new(File.open("server.crt"))
ssl = OpenSSL::SSL::SSLSocket.new(socket)
ssl.sync_close = true
ssl.connect
if ssl.peer_cert.to_s != certificate.to_s
  stderrr.puts "Unexpected certificate"
  exit(1)
end

# Get input through the server
Thread.new {
  begin
    while server_res = ssl.gets.chomp
      puts server_res
    end
  rescue
    $stderr.puts "Error in server response loop: " + $!
  end
}
# send out the STDIN to the server
while (client_sent = STDIN.gets.chomp)
  ssl.puts client_sent 
end

服务器

#!/usr/bin/ruby

require "socket"
require "openssl"
require "thread"

port = Integer(ARGV[0])
server = TCPServer.new(port)

sslCont = OpenSSL::SSL::SSLContext.new
sslCont.cert = OpenSSL::X509::Certificate.new(File.open("server.crt"))
sslCont.key = OpenSSL::PKey::RSA.new(File.open("server.key"))

sslServ = OpenSSL::SSL::SSLServer.new(server, sslCont)

loop {
  connection = sslServ.accept
  Thread.new {
    begin
      while (client_sent = connection.gets) do
        client_sent = client_sent.chomp
        puts "Client Sent: " + client_sent
        server_res = "Server Response: " + client_sent
        puts server_res
        connection.puts server_res
      end
    rescue
      $stderr.puts $!
    end
  }
}

1 个答案:

答案 0 :(得分:2)

  • 保留一系列已接受的连接

    connections = []
    
  • 当有人连接或断开连接时保持最新状态

    connections << connection
    ...
    connections.delete(connection)
    
  • 发送时,您可以发送给所有人:

    connections.each do |conn|
      conn.puts server_res
    end
    
  • 或者除了当前的所有内容之外的所有内容:

    connections.each do |conn|
      next if conn == connection
      conn.puts server_res
    end
    
  • 编辑:从评论中删除,因为它非常重要:将connections.delete放入finally块的begin...rescue...finally...end子句中。这样,当连接因任何原因而中断时(无论是自然地破坏while条件,还是异常地通过异常),它都会被执行。你不能做的就是把它放在线程块之外,因为一旦放入connection,每个connections都会从{{1}}中取出。