【发布时间】:2010-12-31 17:09:15
【问题描述】:
我正在尝试构建一个纯粹使用 Ruby 的聊天应用程序。之前发布了similar question,但我有不同的相关查询。我看过this example(与之前发布类似问题的人所提到的相同)。示例中的代码似乎对我不起作用。在终端上运行 ruby 脚本并在浏览器中连接到 url:http://localhost:1234 时,我无限期地遇到“从本地主机传输数据...”消息。
这里的 1234 是提供的示例中使用的端口号。我无法弄清楚我跑步失败的原因是什么。可能是我需要在执行脚本时在命令行中指定一些东西,或者我应该通过其他地方(可能是浏览器)开始聊天(输入输出)。我无法弄清楚到底该怎么做。你能帮我解决这个问题吗?
我运行的聊天服务器代码几乎没有修改。我在同一主机上运行网络服务和聊天服务器。
我能够部分地让代码为我工作,直到循环开始。下面给出了对我有用的修改代码。
require 'gserver'
class BasicServer < GServer
def initialize(*args)
super(*args)
# Keep an overall record of the client IDs allocated
# and the lines of chat
@@client_id = 0
@@chat = []
end
def serve(io)
# io.puts("Hello world!")
# Increment the client ID so each client gets a unique ID
@@client_id += 1
my_client_id = @@client_id
my_position = @@chat.size
# io.puts(@@chat.size)
# Give the total number of people who are currently on chat.. for e.g. 0 => 1 person on chat
# Leave a message on the chat queue to signify this client
# has joined the chat
@@chat << [my_client_id, ""]
# io.puts(@@chat)
end
end
server = BasicServer.new(1234)
server.start
#sleep 120
#server.shutdown
对于每个浏览器实例,都有一个新客户端连接到聊天队列(它们具有唯一的客户端 ID 来识别它们)。我想通过向正在运行的浏览器实例添加一个文本框(类似于我们在 html 中使用的内容)来重用示例中的代码,用户可以在其中输入他们的消息并使用点击发布它说一个按钮(也集成在浏览器中)。这反映在各种客户端的所有其他浏览器实例中,并且聊天一直如此进行,直到用户输入字符串“quit”以离开聊天室。
我也不确定如何在 Ruby 中实现上述功能,任何建议或相关文章都可以参考。
非常感谢!!
【问题讨论】: