【问题标题】:Websocket in rails not working because of routes由于路线,Rails中的Websocket无法正常工作
【发布时间】:2014-06-28 08:53:32
【问题描述】:

我使用 websocket 在 Rails 中创建 web 应用程序。我在 routes.rb 中创建 add 下面

get '/:username' => 'users#profile', as:"profile"

在此之后,我在 Gemfile 中添加波纹管

gem 'websocket-rails'

然后我在 application.js 中添加以下内容

var dispatcher = new WebSocketRails('localhost:3000/websocket');
channel = dispatcher.subscribe('articles');
channel.bind('new', function(article) {
  console.log('a new article about '+article.id+' arrived!');
})

问题是

localhost:3000/websocket =====> 运行 profile_path (get '/:username' => 'users#profile', as:"profile")

如何在不删除 { get '/:username' => 'users#profile', as:"profile" }.. 的情况下解决我的 websocket 问题?

我是使用 web socket 开发 rails 的新用户。

【问题讨论】:

    标签: ruby ruby-on-rails-4 websocket routes


    【解决方案1】:

    我使用路由约束解决了这个问题:

    get '/:hash', to: 'conversations#show', constraints: {hash: /((?!websocket).)*/}

    除非:hash 不包含字符串'websocket',否则路由将不起作用

    我确信那里有更好的正则表达式,如果我的哈希随机包含字符串“websocket”,它就会失败。

    但是,这解决了我在开发中遇到的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2016-11-26
      • 2014-12-05
      • 2018-01-17
      • 2017-10-15
      相关资源
      最近更新 更多