使用 Ruby 构建聊天应用程序

发布于 2024-10-09 21:00:14 字数 1648 浏览 4 评论 0原文

我正在尝试纯粹使用 Ruby 构建一个聊天应用程序。发布了一个类似问题早些时候,但我有不同且相关的疑问。我看过这个示例(与发布之前有类似的问题)。示例中的代码似乎不适合我。在终端上运行 ruby​​ 脚本并在浏览器中连接到 url:http://localhost:1234 时,我无限期地遇到“从 localhost 传输数据...”消息。

这里 1234 是提供的示例中使用的端口号。我无法弄清楚我跑步失败的原因是什么。可能我需要在执行脚本时在命令行中指定某些内容,或者我应该通过其他地方(可能是浏览器)启动聊天(输入输出)。我不知道到底该怎么做。

我运行的聊天服务器代码几乎没有修改。我在同一主机上运行 Web 服务和聊天服务器。

我能够部分地让代码为我工作,直到循环开始。下面给出了在一定程度上对我有用的修改后的代码。

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 中使用的内容)来重用示例中的代码,其中用户可以输入他们的消息并使用单击来发布它一个按钮(也集成在浏览器中)。这反映在各种客户端的所有其他浏览器实例中,并且聊天继续这样进行,直到用户输入字符串“退出”以离开聊天室。

我也不知道如何在 Ruby 中实现上述功能。

I am trying to build a chat application purely using Ruby. There is a similar question posted earlier, but I have different and related queries. I have looked at this example (same as referred by the person who posted a similar question earlier). The code in the example doesn't seem to be working for me. On running the ruby script on the terminal, and connecting to the url: http://localhost:1234 in my browser, I indefinitely encounter a "Transferring data from localhost..." message.

Here 1234 is the port number used in the example provided. I am not able to figure out what is the reason behind my unsuccessful run. May be I need to specify something in the command line while executing the script or I am supposed to start the chat (input output) through some other place (probably the browser). I am not able to figure out what exactly to do.

I am running the chat server code pretty much unmodified. I am running the web service and the chat server on the same host.

I was able to partially get the code working for me up to the point where the loop starts. The modified code which worked for me up to a certain point is given below.

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

For every browser instance a new client is connected to the chat queue (they have unique client IDs to identify them). I wanted to reuse the code in the example by adding a text box(something similar to what we use in html) to the browser instance(s) running wherein a user(s) can enter their message and post it say using the click of a button (which is also integrated in the browser). This reflects in all other browser instances of various clients and the chat goes on like this until users enter a string "quit" to leave the chat room.

I am not sure how to implement the above feature too in Ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2024-10-16 21:00:14

我猜这是您正在使用的唯一代码。 HTTP(您的网络浏览器与服务器通信时使用的协议)的工作方式是,浏览器连接到您的服务器,发送一些行说明它想要获取哪个页面,它有哪些 cookie 等。然后服务器响应,在大多数情况下,关闭连接。因此,在您的情况下,当您在网络浏览器中连接到聊天服务器时,浏览器会连接,BasicServer#serve 被调用,一些内容被发回,并且网络浏览器关闭连接,因此服务器无法向浏览器发送更多数据。 “修复”此问题的最简单方法是使用不同的方式连接到服务器(telnet 或 nc (netcat))。如果您希望在浏览器中实现此功能,则需要让服务器响应 HTTP 请求,然后返回定期轮询服务器的页面(阅读 AJAX 或 WebSockets)。尽管它不是用 Ruby 编写的,但您可以查看此内容以获取灵感: https://github.com/ry/节点聊天

I'm guessing that this is the only code you're using. The way HTTP (the protocol your web browser uses when talking to your server) works, is that the browser connects to your servers, sends some lines saying which page it wants to get, what cookies it has, etc. The server then responds, and in most cases, closes the connection. So in your case, when you connect to the chat server in your web browser, the browser connects, BasicServer#serve gets called, some stuff is sent back, and the web browser closes the connection, so the server can't send more data to the browser. The easiest way to "fix" this is to use a different way of connecting to your server (either telnet or nc (netcat)). If you want this to be in a browser, you need to make your server respond to HTTP requests, and then return a page that regularly polls the server (read up on AJAX or WebSockets). You can look at this for inspiration, although it isn't written in Ruby: https://github.com/ry/node_chat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文