Flex与Ruby通过socket实现通简易文本聊天

Socket服务器用Ruby语言实现,代码如下:

require "socket"

server = TCPServer.new("localhost", 5000)
sessions = []
while(session = server.accept)
  sessions << session
  Thread.new(session) do |local_session|
    while(true)
      data = local_session.gets
      puts data
      if data.chomp == "quit"
        sessions.delete(local_session)
        local_session.close
      end
      sessions.each do |s|
        s.puts data
      end
    end
  end
end 

flash中有两种类型的socket,分别为XMLSocket和Socket,前者使用起来简单一点,这里我们选用后者,完整代码如下:
(更多…)

Posted in Flex, Ruby. Tags: , , . 没有评论 »