简体   繁体   English

如何在Ruby on Rails中使用TCPServer?

[英]How to use TCPServer in Ruby on Rails?

I am using Ruby on Rails on Cloud9 IDE 我在Cloud9 IDE上使用Ruby on Rails

$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
 $ rails -v
Rails 4.2.4

I have to transmit data between server and GPS device. 我必须在服务器和GPS设备之间传输数据。 I put on device and did code like below. 我在设备上放了代码,如下所示。

My_Controller My_Controller

class DashboardController < ApplicationController

    before_action :authenticate_user!


       def index

        require 'socket'
        server = TCPServer.new ("127.0.0.1",8000)
        loop do
          Thread.start(server.accept) do |client|
            client.puts "Hello !"
            client.puts "Time is #{Time.now}"
            client.close
          end
        end
        @bookings = Booking.where(user: current_user).joins(:customer, :driver, :car)
        @customers = Customer.where(user: current_user)
       end
end` **** 

when i ran this i got 当我跑这个我得到

<html>
  <head>
    <meta charset='utf-8'> 
    <title>Error 502 - Bad Gateway</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" />
    <style type="text/css">
    .error_content {
        background: rgba(255, 255, 255, 0.23);
        padding: 10px;
        width: 641px;
        margin: 25px 0;
        display: none;
    }

    #error-msg {
        display: block;
    }
    </style>
  </head>
  <body class="errorUnknown light">
    <div id="wrapper">
      <h1>Error 502 - Bad Gateway</h1>
      <div class="error_content" id="error-msg">
          <p>Please click <a href="javascript:location.reload(true)">here</a> to try again, if the issue persists please contact <a href="https://c9.io/support">support</a></p>
      </div>

      <a href="http://status.c9.io">Status Page</a> |
      <a href="https://c9.io/support">Support</a> |
      <a href="https://c9.io/dashboard.html">Dashboard</a> |
      <a href="https://c9.io">Home</a>
    </div>
  </body>
</html>

Kindly, help me how to solve this problem.. Thanks in advance. 请帮助我如何解决这个问题。

You go into an endless loop in the action. 您在动作中陷入无休止的循环。 The request from the reverse proxy to your rails server times out. 从反向代理到Rails服务器的请求超时。 The reverse proxy sends an error message (502) to the browser. 反向代理将错误消息(502)发送到浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM