Can I start separate port on heroku for EventMachine?
我有一个ruby on rails应用程序,其中我为EventMachine定义了一个初始化程序,它将在指定端口上启动EventMachine套接字,这是初始化程序websocket.rb的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Thread.new { require 'eventmachine' require 'em-websocket' EventMachine.run { host ="0.0.0.0" port = 2000 $CHANNEL = EventMachine::Channel.new EventMachine::WebSocket.start(:host => host, :port => port, :debug => true) do |ws| ws.onopen { @sid = $CHANNEL.subscribe { |msg| ws.send msg } # $CHANNEL.push"#{@sid} connected!" } ws.onmessage { |msg| $CHANNEL.push"#{msg}" } ws.onclose { puts"Socket closed." # this code is commented as we don't want to unsubscribe any socket from channel # $CHANNEL.unsubscribe(@sid) } end puts"Socket server started..." } } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive? |
我已经在html文件中监听了这个套接字,我在客户端使用了html WebSocket。
这是我的客户端websocket侦听器的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | if("WebSocket" in window){ var socketURL = 'ws://' + window.location.hostname + ':2000/'; console.log(socketURL); ws = new WebSocket(socketURL); ws.onmessage = function(response) { console.log('Data received:'+response.data); } ws.onclose = function() { console.info('Connection closed'); }; ws.onopen = function() { console.info('Connection opened'); }; }else{ console.log("You browser doesn't support websockets.") } |
我想将此Rails应用程序部署到Heroku,那么Heroku的EventMachine是否有问题?
是否可以在Heroku上为TCPServer启动端口?
如果我在Heroku上部署此应用程序,可能会出现什么问题?
任何建议或帮助将不胜感激。
预先感谢。
否,无法在heroku上打开端口以运行事件机Web套接字(如您的问题中所述)。路由层将仅将端口80和443代理到您的应用程序,当您的应用程序启动时,端口将侦听heroku提供的任意端口。
客户端将无法连接到您指定端口上的Web套接字。参见此处,了解将Web套接字与heroku一起使用的示例:https://devcenter.heroku.com/articles/websockets