Session 将Kemal会话与websocket一起使用

Session 将Kemal会话与websocket一起使用,session,websocket,crystal-lang,kemal,Session,Websocket,Crystal Lang,Kemal,Crystal中kemal web框架的kemal会话模块文档提供了以下内容: 我在用凯末尔。我有一个类似于以下示例的代码。如果我没有访问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 d

Crystal中kemal web框架的kemal会话模块文档提供了以下内容:

我在用凯末尔。我有一个类似于以下示例的代码。如果我没有访问
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

Websocket连接也会生成上下文。你只需要像这样从块访问它

ws "/" do |socket, env|
  env.session.int?("hello")
end
ws "/" do |socket, env|
  env.session.int?("hello")
end