如何使用 ruby​​ Gserver 构建基于网络的聊天系统

发布于 2024-08-12 20:56:28 字数 290 浏览 4 评论 0原文

我正在尝试构建一个基于网络的聊天系统,并且我将使用 ruby​​ gserver。我看过 这个示例 。然而我的问题是,当我从网络获取用户输入时,在控制器中我有用户输入。现在客户端如何连接到服务器以将用户输入值传递到服务器。

服务器获得该值后将填充数据库。因此客户端将从数据库执行所有读取操作。不过我想知道客户端如何连接到服务器。这是一个简单的问题,但我无法弄清楚。

I am trying to build a web based chat system and I am going to user ruby gserver. I have looked at this example . However my question is when I get the user input from the web and in the controller I have the user input. Now how does client connect to server to pass this user input value to the server.

The server after getting the value will populate a database. So the client will do all read operations from database. However I was wondering how will client connect to server. It is a simple question but I could not figure it out.

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

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

发布评论

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

评论(1

梦中楼上月下 2024-08-19 20:56:28

现在,我正在做出一些巨大的假设,因为你的问题非常模糊。

假设 1:您正在运行几乎未修改的聊天服务器
假设 2:您在同一主机上运行 Web 服务和聊天服务器

在这种情况下,您可以使用套接字库连接到聊天服务器,并以这种方式向其发送数据。

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 1234, 'localhost' )
socket.connect( sockaddr )
socket.write( "foo\nquit\n" )
puts socket.read
socket.close

这会将“foo”发送到聊天服务器,然后关闭连接

Now, I'm making some massive assumptions, because your question is as vague as hell.

Assumption 1: You are running the chatserver pretty much unmodified
Assumption 2: You are running the web service and the chat server on the same host

In that case, you can connect to the chat server using the socket libs, and send it data that way.

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 1234, 'localhost' )
socket.connect( sockaddr )
socket.write( "foo\nquit\n" )
puts socket.read
socket.close

This will send "foo" to the chat server, and then close the connection

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