如何在我的 Ruby on Rails 应用程序中使用 Redis 和 Windows OS?
How can I use Redis in my Ruby on Rails application with Windows OS?
首先,对于我的新手问题,我深表歉意,但我有点被困在这里了。
如果我有 Windows OS,谁能逐步告诉我如何在我的 Rails6 应用程序中使用 Redis?
我已经安装了 Redis,目前它在我的 C:\Program Files 中,
with these files inside it
我启动了 redis-server.exe,当它启动时 运行 它说:
[8020] 20 Nov 17:26:06 # Warning: no config file specified, using the default config.
In order to specify a config file use 'redis-server /path/to/redis.conf'
[8020] 20 Nov 17:26:06 * Server started, Redis version 2.4.6
[8020] 20 Nov 17:26:06 # Open data file dump.rdb: No such file or directory
[8020] 20 Nov 17:26:06 * The server is now ready to accept connections on port 6
379
[8020] 20 Nov 17:26:07 - 0 clients connected (0 slaves), 672768 bytes in use
[8020] 20 Nov 17:26:12 - 0 clients connected (0 slaves), 672768 bytes in use
[8020] 20 Nov 17:26:17 - 0 clients connected (0 slaves), 672768 bytes in use
........(它只是每 5 秒输出一次相同的文本)
同样在我的 Rails 应用程序中,我配置了一些东西。我将 config/cable.yml 文件更改为:
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: actioncable_test_production
我的想法是创建一个名为 'room' 的通道,并在控制台中输出一条消息,以查看其是否已成功连接。但我的问题是它没有输出任何东西。
我将 app/javascript/channels/room_channel.js 设置为:
import consumer from "./consumer"
consumer.subscriptions.create("RoomChannel", {
connected() {
// Called when the subscription is ready for use on the server
console.log("Connected succesfully!")
},
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
}
});
和我的 app/channels/room_channel.rb 文件到:
class RoomChannel < ApplicationCable::Channel
def subscribed
stream_from "room_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
此外,在我的命令提示符上显示:(在通常的渲染消息和类似的东西之后):
Started GET "/cable" for 127.0.0.1 at 2021-11-20 18:24:05 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2021-11-20 18:24:06 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-a
live, Upgrade, HTTP_UPGRADE: websocket)
当我转到浏览器控制台时,它说 Firefox 无法创建与 ws://localhost:3000/cable 的连接
知道这里有什么问题吗?
感谢您的回答和最诚挚的问候,Rererbit!
您使用的是非常旧且已弃用的 Redis 版本,这是 Microsoft 废弃的名为 OpenTech Redis 的项目。
我建议您查看 Memurai。
Memurai 是 Redis 的最新本地 Windows 端口,派生自该项目(参见 this commit)。
Memurai 还提供免费的开发者版。
免责声明:我在 Memurai 工作。
首先,对于我的新手问题,我深表歉意,但我有点被困在这里了。 如果我有 Windows OS,谁能逐步告诉我如何在我的 Rails6 应用程序中使用 Redis? 我已经安装了 Redis,目前它在我的 C:\Program Files 中, with these files inside it
我启动了 redis-server.exe,当它启动时 运行 它说:
[8020] 20 Nov 17:26:06 # Warning: no config file specified, using the default config.
In order to specify a config file use 'redis-server /path/to/redis.conf'
[8020] 20 Nov 17:26:06 * Server started, Redis version 2.4.6
[8020] 20 Nov 17:26:06 # Open data file dump.rdb: No such file or directory
[8020] 20 Nov 17:26:06 * The server is now ready to accept connections on port 6
379
[8020] 20 Nov 17:26:07 - 0 clients connected (0 slaves), 672768 bytes in use
[8020] 20 Nov 17:26:12 - 0 clients connected (0 slaves), 672768 bytes in use
[8020] 20 Nov 17:26:17 - 0 clients connected (0 slaves), 672768 bytes in use
........(它只是每 5 秒输出一次相同的文本)
同样在我的 Rails 应用程序中,我配置了一些东西。我将 config/cable.yml 文件更改为:
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: actioncable_test_production
我的想法是创建一个名为 'room' 的通道,并在控制台中输出一条消息,以查看其是否已成功连接。但我的问题是它没有输出任何东西。
我将 app/javascript/channels/room_channel.js 设置为:
import consumer from "./consumer"
consumer.subscriptions.create("RoomChannel", {
connected() {
// Called when the subscription is ready for use on the server
console.log("Connected succesfully!")
},
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
}
});
和我的 app/channels/room_channel.rb 文件到:
class RoomChannel < ApplicationCable::Channel
def subscribed
stream_from "room_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
此外,在我的命令提示符上显示:(在通常的渲染消息和类似的东西之后):
Started GET "/cable" for 127.0.0.1 at 2021-11-20 18:24:05 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2021-11-20 18:24:06 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-a
live, Upgrade, HTTP_UPGRADE: websocket)
当我转到浏览器控制台时,它说 Firefox 无法创建与 ws://localhost:3000/cable 的连接
知道这里有什么问题吗? 感谢您的回答和最诚挚的问候,Rererbit!
您使用的是非常旧且已弃用的 Redis 版本,这是 Microsoft 废弃的名为 OpenTech Redis 的项目。
我建议您查看 Memurai。 Memurai 是 Redis 的最新本地 Windows 端口,派生自该项目(参见 this commit)。
Memurai 还提供免费的开发者版。
免责声明:我在 Memurai 工作。