rails实现论坛_如何在Rails 4中实现WebSockets

rails实现论坛

by Arun Mathew Kurian

通过阿伦·马修·库里安(Arun Mathew Kurian)

如何在Rails 4中实现WebSockets (How to implement WebSockets in Rails 4)

WebSockets can be implemented in Rails5 using ActionCable. It can be used for enabling many features like chats, notifications, and other real-time modules. But, how does one achieve the same goal without ActionCable and Rails 5? This blog deals with how we can implement WebSockets in Rails 4.

WebSockets可以使用ActionCable在Rails5中实现。 它可以用于启用许多功能,例如聊天,通知和其他实时模块。 但是,如果没有ActionCable和Rails 5,如何实现相同的目标呢? 该博客介绍了如何在Rails 4中实现WebSockets。

Before we start, let’s have a quick look at the concept of WebSockets.

在开始之前,让我们快速了解一下WebSockets的概念。

什么是WebSocket,它们如何工作? (What are WebSockets and how do they work?)

The majority of the web is based on HTTP request/responses. They enable the communication of many hosts and clients over TCP/IP transports. This means that web applications usually don’t do anything after a user visits them in a browser and parses the request and sends a response. Until the user clicks something on the page, the server won’t receive any new HTTP request. Therefore it will stand idle.

Web的大部分基于HTTP请求/响应。 它们允许通过TCP / IP传输进行许多主机和客户端的通信。 这意味着,在用户在浏览器中访问Web应用程序并解析请求并发送响应之后,Web应用程序通常不执行任何操作。 除非用户单击页面上的某些内容,否则服务器将不会收到任何新的HTTP请求。 因此它将处于空闲状态。

There are technologies that enable the server to start communication with the client when there is data to be sent. Examples are “Push” or “Comet”. There is the long polling technique which keeps an HTTP connection open once a client connects to the server. The bad thing with these approaches is the overhead of HTTP. It isn’t very good for low latency applications.

有一些技术可以使服务器在要发送数据时开始与客户端进行通信。 例如“推”或“彗星”。 客户端使用长轮询技术可以在客户端连接到服务器后保持HTTP连接打开。 这些方法的坏处是HTTP的开销。 对于低延迟的应用程序,它不是很好。

This is where WebSockets come into the picture. They are an API that provides “socket” persistent connections between a server and a client. This enables both server and client to send data at any time.

这就是WebSocket出现的地方。 它们是一种API,可在服务器和客户端之间提供“套接字”持久连接。 这使服务器和客户端都可以随时发送数据。

In this blog, we will create an online cricket player auction site that uses web sockets in Rails 4. The site can be used by multiple users to bid for the same player. The challenge is to update the bid without reloading the page and keeping up the live communication.

在此博客中,我们将创建一个在线板球玩家拍卖网站,该网站在Rails 4中使用Web套接字。该网站可以被多个用户用来竞标同一位玩家。 挑战在于在不重新加载页面和保持实时交流的情况下更新出价。

入门 (Getting started)

Mainly three gems are used for implementing the web socket functionality:

主要使用三个gem来实现Web套接字功能:

Gemfile

宝石文件

gem ‘faye’gem ‘thin’, require: falsegem ‘render_sync’

The thin is a small and fast ruby server. It should be installed with faye, as the faye gem doesn’t work with servers like webrick.

服务器是小型快速的ruby服务器。 它应该与faye一起安装,因为faye gem无法与webrick之类的服务器一起使用。

The next important gem is faye. Faye is a set of tools for simple publish-subscribe messaging between web clients. It ships with easy-to-use message routing servers for Node.js and Rack applications, and clients that can be used on the server and in the browser.

下一个重要的宝石是王菲 。 Faye是用于在Web客户端之间进行简单的发布-订阅消息传递的一组工具。 它附带了用于Node.js和Rack应用程序的易于使用的消息路由服务器,以及可以在服务器和浏览器中使用的客户端。

The sync or render_sync gem is used to create real-time partials with Rails. Sync lets you render partials for models that, with minimal code, update in real-time in the browser when changes occur on the server.

syncrender_sync gem用于通过Rails创建实时局部 。 通过Sync,您可以为模型渲染零件,这些零件只需最少的代码,即可在服务器上发生更改时在浏览器中实时更新。

Our objective is to have a functionality which allows displaying the bid values on the show page of a user. The first step for implementing this is to install templates from the sync gem.

我们的目标是提供一种功能,允许在用户的显示页面上显示出价值。 实施此操作的第一步是从sync gem安装模板。

rails generate sync:install

rails生成sync:install

And require sync in our asset pipeline.

并要求在我们的资产管道中进行同步。

app/assets/javascripts/application.js

app / assets / javascripts / application.js

//= require jquery//= require jquery_ujs//= require turbolinks//= require sync//= require_tree

The configuration script is required in the application layout

应用程序布局中需要配置脚本

app/views/layouts/application.html.erb

app / views / layouts / application.html.erb

<%= include_sync_config %>

We need to create a partial and store it in the directory views/sync/ as _bid_log_row.html.erb.

我们需要创建一个局部文件,并将其作为_bid_log_row.html.erb存储在目录views / sync /

This partial contains the value of the user’s bid. It will look like this:

此部分包含用户出价的值。 它看起来像这样:

Current Bid: <%= @bid_log.amount || ‘ — ‘ rescue nil%>

And in order to render this in the show page, add the following lines in the users show page:

并且为了在显示页面上呈现此内容,请在用户显示页面中添加以下几行:

app/views/users/show.html.erb

app / views / users / show.html.erb

<%= sync partial: ‘bid_log_row’, resource: @bid_log %><%= sync_new partial: ‘bid_log_row’, resource: BidLog.new %>

And lastly, make the changes in the BidLogsController so it knows how to handle remote form submissions. It also syncs the new bids in place.

最后,在BidLogsController中进行更改,以便它知道如何处理远程表单提交。 它还会同步新的出价。

class BidLogsController < ApplicationControllerrespond_to :html, :js
def index  @bid_logs = BidLog.all  @new_bid = current_user.bid_logs.build  end
def create  @bid_log = current_user.bid_logs.build(bid_log_params)  if @bid_log.save   sync_new @bid_log  end  respond_to do |format|   format.html { redirect_to user_path(@bid_log.player_id) }   format.json { head :no_content }  end end
private
def bid_log_params  params.require(:bid_log).permit(:amount, :player_id) end
end

组态 (Configuration)

Now the basic coding part is done. Next step is to configure Faye. Faye needs to run on a separate web server from the web application itself. To accomplish this, you need to create a Rackup config file. Add a faye.ru file to the root of the project and make sure it looks like this:

现在基本的编码部分已经完成。 下一步是配置Faye。 Faye需要在与Web应用程序本身不同的Web服务器上运行。 为此,您需要创建一个Rackup配置文件。 将faye.ru文件添加到项目的根目录,并确保它看起来像这样:

require ‘faye’
bayeux = Faye::RackAdapter.new(:mount => ‘/faye’, :timeout =&gt; 25)
bayeux.listen(9292)

This file simply tells Rackup how to start the Faye server. Try it out to ensure that it’s working correctly. Run this in your Terminal:

该文件仅告诉Rackup如何启动Faye服务器。 尝试一下以确保其正常工作。 在您的终端中运行此命令:

rackup faye.ru -E production -s thin

机架faye.ru -E生产-s薄

结语 (Wrapping up)

Now we are good to go. The application can be run by starting the Rails server. The code associated with this blog can be found here.

现在我们很好。 可以通过启动Rails服务器来运行该应用程序。 与该博客关联的代码可以在这里找到。

翻译自: https://www.freecodecamp.org/news/implementing-web-sockets-in-a-rails-4-fb45696f8d8c/

rails实现论坛

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值