- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在带 Action 电缆的 Rails 中,我无法从客户端向服务器获取消息,尽管我可以从服务器发送到 channel 再到客户端。
你可以在这里看到所有文件,最后我用 cat 展示了它们,因为我展示了在制作程序时使用的所有命令
查看 awpchan_channel.rb 我已经 def 订阅了,这有效,其中的一个 puts 命令将写入服务器。但是 def receive(data) 永远不会运行。我在那里写了一个 puts 但它没有执行。
但我在 js 文件中确实有一行用于从客户端向服务器发送字符串。 App.awpchan.send('This is a cool chat app.');
但它并没有这样做。
我确实在 coffeescript 中有它,但也没有这样做。
config/routes.rb
Rails.application.routes.draw do
mount ActionCable.server, at: '/cable'
root 'application#index'
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def index
ActionCable.server.broadcast 'abcdstrm', 'zzz'
end
end
app/channels/awpchan_channel.rb
class AwpchanChannel < ApplicationCable::Channel
def subscribed
stream_from "abcdstrm"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def receive(data)
puts "RECEIVE BY SERVER"
end
end
app/assets/javascripts/channels/awpchan.coffee(我将其文件名和扩展名重命名为 oldawpchan.oldcoffee,因为我没有使用它,我正在使用 theawpchan.js)
App.awpchan = App.cable.subscriptions.create "AwpchanChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
alert(data)
App.awpchan.send 'This is a cool chat app.'
app/assets/javascripts/theawpchan.js
App.awpchan = App.cable.subscriptions.create("AwpchanChannel", {
connected: function() {},
disconnected: function() {},
received: function(data) {
return alert(data);
}
});
App.awpchan.send('This is a cool chat app.');
// http://js2.coffee/
app/views/application/index.html.erb(空白)
这是我创建程序的方式
~/rubymac$ cd channeltests
~/rubymac/channeltests$ mkdir channeltest1
~/rubymac/channeltests$ cd channeltest1
~/rubymac/channeltests/channeltest1$ rails new . >nul
~/rubymac/channeltests/channeltest1$ vi config/routes.rb
~/rubymac/channeltests/channeltest1$ cat config/routes.rb
Rails.application.routes.draw do
mount ActionCable.server, at: '/cable'
root 'application#index'
end
~/rubymac/channeltests/channeltest1$ mkdir app/views/application
~/rubymac/channeltests/channeltest1$ touch app/views/application/index.html.erb
~/rubymac/channeltests/channeltest1$ vi app/controllers/application_controller.rb
~/rubymac/channeltests/channeltest1$ cat app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def index
ActionCable.server.broadcast 'abcdstrm', 'zzz'
end
end
~/rubymac/channeltests/channeltest1$ rails generate channel awpchan
Running via Spring preloader in process 4020
create app/channels/awpchan_channel.rb
identical app/assets/javascripts/cable.js
create app/assets/javascripts/channels/awpchan.coffee
~/rubymac/channeltests/channeltest1$ vi app/channels/awpchan_channel.rb
~/rubymac/channeltests/channeltest1$ cat app/channels/awpchan_channel.rb
class AwpchanChannel < ApplicationCable::Channel
def subscribed
stream_from "abcdstrm"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def receive(data)
puts "RECEIVE BY SERVER"
end
end
~/rubymac/channeltests/channeltest1$ vi app/assets/javascripts/channels/awpchan.coffee
~/rubymac/channeltests/channeltest1$ cat app/assets/javascripts/channels/awpchan.coffee
App.awpchan = App.cable.subscriptions.create "AwpchanChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
alert(data)
App.awpchan.send 'This is a cool chat app.'
~/rubymac/channeltests/channeltest1$ cd app/assets/javascripts/channels/
~/rubymac/channeltests/channeltest1/app/assets/javascripts/channels$ mv awpchan.coffee oldawpchan.oldcoffee
~/rubymac/channeltests/channeltest1/app/assets/javascripts/channels$ vi oldawpchan.oldcoffee
~/rubymac/channeltests/channeltest1/app/assets/javascripts/channels$ vi theawpchan.js
~/rubymac/channeltests/channeltest1/app/assets/javascripts/channels$ cat theawpchan.js
App.awpchan = App.cable.subscriptions.create("AwpchanChannel", {
connected: function() {},
disconnected: function() {},
received: function(data) {
return alert(data);
}
});
App.awpchan.send('This is a cool chat app.');
// http://js2.coffee/
~/rubymac/channeltests/channeltest1/app/assets/javascripts/channels$ cd ../../../
~/rubymac/channeltests/channeltest1/app$ cd ..
~/rubymac/channeltests/channeltest1$ rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/" for 127.0.0.1 at 2019-04-21 23:40:55 +0100
Processing by ApplicationController#index as HTML
[ActionCable] Broadcasting to abcdstrm: "zzz"
Rendering application/index.html.erb within layouts/application
Rendered application/index.html.erb within layouts/application (1.5ms)
Completed 200 OK in 469ms (Views: 430.1ms | ActiveRecord: 0.0ms)
Started GET "/cable" for 127.0.0.1 at 2019-04-21 23:41:25 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2019-04-21 23:41:25 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
AwpchanChannel is transmitting the subscription confirmation
AwpchanChannel is streaming from abcdstrm
已添加
应用 arieljuod 的建议,在 js 文件中,将发送行放在连接方法的主体中。这样它就不会发送得太早。
这改善了事情,现在我收到了这条消息
就在服务员说完
AwpchanChannel is transmitting the subscription confirmation
AwpchanChannel is streaming from abcdstrm
它现在给出这个错误(这将是发送命令执行时)
Could not execute command from ({"command"=>"message", "identifier"=>"{\"channel\":\"AwpchanChannel\"}", "data"=>"\"This is a cool chat app.\""}) [NoMethodError - undefined method `except' for "This is a cool chat app.":String]: /usr/local/lib/ruby/gems/2.5.0/gems/actionc...
最佳答案
第一个问题是您在 channel 正确初始化之前调用了 send
方法,请先等待它连接。
第二件事是 send
方法需要一个具有参数名称的对象,以便服务器可以以某种方式识别它:
App.awpchan.send({message: 'This is a cool chat app.'});
App.awpchan = App.cable.subscriptions.create("AwpchanChannel", {
connected: function() {
App.awpchan.send({message: 'This is a cool chat app.'});
},
disconnected: function() {},
received: function(data) {
return alert(data);
}
});
// http://js2.coffee/
由 barlop 补充说明
在服务器端你可以做
def received
puts data["message"]
end
关于ruby-on-rails - 在带有 Action 电缆的 rails 中,无法从客户端发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787386/
Rails 5 引入了 Action Cable,但在 Rails 4 中有实时流媒体。所以我很困惑两者有什么区别,Live Streaming 在 rails 5 中已经死了吗?或 Action C
我有一个很奇怪的问题。最近我尝试使用以下代码在我的应用程序中使用唤醒锁: /** Called when the activity is first created. */ @Override
我在我的应用程序中设置了一个基本的行动电缆供电聊天。我有一个 after_create_commit 回调,它将消息发送到要广播到适当 channel 的作业。当它设置为 perform_now 时它
我在使用 Windows Phone 7.5 的 HTC Titan 上运行一个非常简单的 TCP 客户端时遇到了一些麻烦。 当 USB 数据线连接到手机上时,TCP 客户端工作得很好,但是一旦拔下数
您好,我已经使用 rails 5 实现了 action cable。它在本地工作正常,但在 heroku 上不工作。在 heroku 上,每当我发送任何消息时,它都会发送四条消息,两条重复消息和两条空
在聊天中传递错过的消息的好方法是什么,例如使用 Action Cable?就像当您关闭笔记本电脑并再次打开它时,您并没有重新加载页面,但您可能错过了消息 一种方法是使用 Action Cable 进行
我正在尝试在 websocket 连接上实现授权(rails 5.2.1) 按照 rubyonrails guideline ,我创建了connection.rb如下: # app/channels/
我正在尝试使 kable 表具有反应性并将其导出到 Shiny 的应用程序中。已经尝试在服务器内使用 renderDataTable/renderTable 并将输出函数作为 datatableOut
是否可以将 USB 打印机与 Epson ePos Javascript SDK 一起使用?还是仅适用于以太网/无线型号? 我希望找到一个实用程序将 USB 打印机转换为网络监听打印机,以回收我所有的
我正在使用 ActionCable,并在每 3 秒间隔后从服务器接收 ping(在 ActionCable 库中提到)。我的问题是:如何更改订阅时的 ping 间隔? 有什么想法吗? 最佳答案 扩展@
我正在尝试使用 ISO 9141-2 协议(protocol)从 ECU 读取数据。我使用的电缆是OBD2转USB,使用FT232R芯片。我正在运行的程序是用 C 编写的。 当我向串行端口(ttyUS
我是一名优秀的程序员,十分优秀!