Crystal 中 Kemal Web 框架的 kemal-session 模块的文档提供了以下示例:
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
?
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