gpt4 book ai didi

session - 将 Kemal session 与 websocket 结合使用

转载 作者:行者123 更新时间:2023-12-02 01:18:10 24 4
gpt4 key购买 nike

Crystal 中 Kemal Web 框架的 kemal-session 模块的文档提供了这个 example :

require "kemal"
require "kemal-session"

get "/set" do |env|
env.session.int("number", rand(100)) # set the value of "number"
"Random number set."
end

get "/get" do |env|
num = env.session.int("number") # get the value of "number"
env.session.int?("hello") # get value or nil, like []?
"Value of random number is #{num}."
end

Kemal.run

我正在使用 Kemal 和 Websocket .我有一个类似于以下示例的代码。如果我无权访问 env,我该如何使用 session ?

ws "/" do |socket|
# Send welcome message to the client
socket.send "Hello from Kemal!"

# Handle incoming message and echo back to the client
socket.on_message do |message|
socket.send "Echo back from server #{message}"
end

# Executes when the client is disconnected. You can do the cleaning up here.
socket.on_close do
puts "Closing socket"
end
end

最佳答案

Websocket 连接也产生上下文。您只需要像

这样从 block 中访问它
ws "/" do |socket, env|
env.session.int?("hello")
end

关于session - 将 Kemal session 与 websocket 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com